MongoDBでObjectIDのドキュメントを検索しますか?
MongoDBでObjectidを含むドキュメントを検索するには、次の構文を使用します-
db.yourCollectionName.find({"_id":ObjectId("yourObjectIdValue")}).pretty(); 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-
> db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Larry"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c90e4384afe5c1d2279d69b")
}
> db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Mike","UserAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c90e4444afe5c1d2279d69c")
}
> db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Carol","UserAge":26,"UserHobby":["Learning","Photography"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c90e4704afe5c1d2279d69d")
} find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-
> db.findDocumentWithObjectIdDemo.find().pretty();
以下は出力です-
{ "_id" : ObjectId("5c90e4384afe5c1d2279d69b"), "UserName" : "Larry" }
{
"_id" : ObjectId("5c90e4444afe5c1d2279d69c"),
"UserName" : "Mike",
"UserAge" : 23
}
{
"_id" : ObjectId("5c90e4704afe5c1d2279d69d"),
"UserName" : "Carol",
"UserAge" : 26,
"UserHobby" : [
"Learning",
"Photography"
]
} ケース1- これは、MongoDBでObjectIdを持つドキュメントを検索するためのクエリです。
> db.findDocumentWithObjectIdDemo.find({"_id":ObjectId("5c90e4704afe5c1d2279d69d")}).pretty(); 以下は出力です-
{
"_id" : ObjectId("5c90e4704afe5c1d2279d69d"),
"UserName" : "Carol",
"UserAge" : 26,
"UserHobby" : [
"Learning",
"Photography"
]
} ケース2- これは、MongoDBでObjectIdを持つ別のドキュメントを見つけるためのクエリです。
クエリは次のとおりです-
> db.findDocumentWithObjectIdDemo.find({"_id": ObjectId("5c90e4444afe5c1d2279d69c")}).pretty(); 以下は出力です-
{
"_id" : ObjectId("5c90e4444afe5c1d2279d69c"),
"UserName" : "Mike",
"UserAge" : 23
} -
ネストされたドキュメントのMongoDBfind()クエリ?
ネストされたドキュメントから値をフェッチするには、ドット表記を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo591.insert([ ... { "Name": "John", "Age": 23 }, ... {"Name": "Carol", "Age": 26}, ... { "Name": "Robert",
-
価格が特定の値未満のMongoDBレコードを検索する
価格が特定の値未満のレコードを確認するには、$ltを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo728.insertOne({Price:75}); { "acknowledged" : true, "insertedId" : ObjectId("5eab413c43417811278f589b") } > db.demo728.insertOne({Price:59}); { "acknowl