MongoDBの配列に埋め込まれたドキュメントにフィールドを追加しますか?
これには、$演算子とともにupdate()を使用できます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.addAFieldDemo.insertOne(
... {
...
... "ClientName" : "Larry",
... "ClientCountryName" : "US",
... "ClientOtherDetails" : [
... {
... "ClientProjectName":"Online Banking System"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd44bdc2cba06f46efe9ee8")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.addAFieldDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"),
"ClientName" : "Larry",
"ClientCountryName" : "US",
"ClientOtherDetails" : [
{
"ClientProjectName" : "Online Banking System"
}
]
} 以下は、配列内の埋め込みドキュメントにフィールドを追加するためのクエリです-
> db.addAFieldDemo.update({ClientOtherDetails:{$elemMatch:{"ClientProjectName" : "Online Banking System"}}},
... {$set :{'ClientOtherDetails.$.isMarried':true}},true);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 上記のコレクションのすべてのドキュメントを表示しましょう-
> db.addAFieldDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"),
"ClientName" : "Larry",
"ClientCountryName" : "US",
"ClientOtherDetails" : [
{
"ClientProjectName" : "Online Banking System",
"isMarried" : true
}
]
} -
idがドキュメントフィールドの配列値と等しい場合に除外するMongoDBクエリ
このために、$inと一緒に$notを使用します。ドキュメントを使用してコレクションを作成しましょう- [ { id: "101", subjectid: [ "102" ] }, { id: "102", &nb
-
既存のMongoDBドキュメントに特定のデータ型(リスト、オブジェクト)のフィールドを追加するにはどうすればよいですか?
$setを使用できます。ドキュメントを使用してコレクションを作成しましょう- > db.demo732.insertOne({_id:1,Language:"English"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo732.insertOne({_id:2,Language:"Hindi"}); { "acknowledged" : true, "insertedId" : 2 } find