ANDとORを組み合わせるMongoDBクエリ?
MongoDBでANDとORを組み合わせるには、最初にドキュメントを含むコレクションを作成しましょう-
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"John","StudentAge":23,"StudentSkill":"MongoDB"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd306dcb64f4b851c3a13e2")
}
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"Larry","StudentAge":21,"StudentSkill":"MySQL"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd306f3b64f4b851c3a13e3")
}
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"Sam","StudentAge":24,"StudentSkill":"SQL Server"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd30701b64f4b851c3a13e4")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.combinedAndOrDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd306dcb64f4b851c3a13e2"),
"StudentFirstName" : "John",
"StudentAge" : 23,
"StudentSkill" : "MongoDB"
}
{
"_id" : ObjectId("5cd306f3b64f4b851c3a13e3"),
"StudentFirstName" : "Larry",
"StudentAge" : 21,
"StudentSkill" : "MySQL"
}
{
"_id" : ObjectId("5cd30701b64f4b851c3a13e4"),
"StudentFirstName" : "Sam",
"StudentAge" : 24,
"StudentSkill" : "SQL Server"
} 以下は、ANDとORの組み合わせのクエリです-
> db.combinedAndOrDemo.find( {"$or":[ {"$and": [{"StudentFirstName": "John"}, {"_id": ObjectId("5cd306dcb64f4b851c3a13e2")}] }, {"StudentSkill" : "MongoDB" } ] } ); これにより、次の出力が生成されます-
{ "_id" : ObjectId("5cd306dcb64f4b851c3a13e2"), "StudentFirstName" : "John", "StudentAge" : 23, "StudentSkill" : "MongoDB" } -
ファイル名と場所を保存するMongoDBクエリ?
保存するには、例を見て、ドキュメントを使用してコレクションを作成しましょう- > db.demo645.insertOne( ... { ... 'fileName' : 'MongoDB Program', ... 'fileLocation':'C:/users/workspace/AllMongoDBProgram/MongoDB Program' ... } ... ); { &nb
-
個別を選択してカウントするMongoDBクエリ?
ドキュメントを使用してコレクションを作成しましょう- > db.demo586.insertOne( ... {"details": [ ... { ... "Name":"Chris", ... "Marks":71 ... }, ...