MongoDBコレクションから特定のキーと値のペアを読み取る方法は?
dot(。)表記を使用して、MongoDBコレクションから特定のキーと値のペアを読み取ることができます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.readSpecificKeyValueDemo.insertOne(
... {
... "_id": 100,
... "StudentDetails":
... {
... "StudentFirstName" : "David",
... "StudentLastName" :"Miller",
... "StudentAge":23,
... "StudentCountryName":"US"
... }
... }
... );
{ "acknowledged" : true, "insertedId" : 100 } 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.readSpecificKeyValueDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : 100,
"StudentDetails" : {
"StudentFirstName" : "David",
"StudentLastName" : "Miller",
"StudentAge" : 23,
"StudentCountryName" : "US"
}
} 以下は、MongoDBコレクションから特定のキーと値のペアを読み取るためのクエリです-
> db.readSpecificKeyValueDemo.find({},{"StudentDetails.StudentCountryName":1}).pretty(); これにより、次の出力が生成されます-
{ "_id" : 100, "StudentDetails" : { "StudentCountryName" : "US" } } -
MongoDBコレクションから一意の値を取得するにはどうすればよいですか?
一意の値を取得して重複を無視するには、MongoDBでdistinct()を使用します。個別の()は、単一のコレクション全体で指定されたフィールドの個別の値を検索し、結果を配列で返します。 ドキュメントを使用してコレクションを作成しましょう- > db.demo704.insertOne({"LanguageCode":"hi"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee
-
MongoDBコレクションから重複を削除するにはどうすればよいですか?
このために、「 unique:true」を設定します 」つまり、一意の制約を使用し、次の構文のように重複を挿入しないようにします- db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true}) 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ここでは、重複挿入は許可されません- > db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{ &nb