MongoDBの配列の要素と一致しますか?
$ or演算子をlimit(1)とともに使用して、配列内の要素を照合できます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.matchElementInArrayDemo.insertOne(
... {
... "StudentName" : "Chris" ,
... "StudentOtherDetails" :
... [
... {"StudentCountryName" : "US" , "StudentSkills" : "MongoDB"},
... {"StudentCountryName" : "UK" , "StudentSkills" : "Java"}
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd423282cba06f46efe9ee2")
}
> db.matchElementInArrayDemo.insertOne(
... {
... "StudentName" : "Chris" ,
... "StudentOtherDetails" :
... [
... {"StudentCountryName" : "AUS" , "StudentSkills" : "PHP"},
... {"StudentCountryName" : "US" , "StudentSkills" : "MongoDB"}
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd423412cba06f46efe9ee3")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.matchElementInArrayDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd423282cba06f46efe9ee2"),
"StudentName" : "Chris",
"StudentOtherDetails" : [
{
"StudentCountryName" : "US",
"StudentSkills" : "MongoDB"
},
{
"StudentCountryName" : "UK",
"StudentSkills" : "Java"
}
]
}
{
"_id" : ObjectId("5cd423412cba06f46efe9ee3"),
"StudentName" : "Chris",
"StudentOtherDetails" : [
{
"StudentCountryName" : "AUS",
"StudentSkills" : "PHP"
},
{
"StudentCountryName" : "US",
"StudentSkills" : "MongoDB"
}
]
} これがMongoDBの配列の要素に一致するクエリです-
> db.matchElementInArrayDemo.find( { $or : [ {"StudentOtherDetails.StudentCountryName": "US" } ,{"StudentOtherDetails.StudentSkills": "MongoDB" } ] } ).limit(1);> これにより、次の出力が生成されます-
{ "_id" : ObjectId("5cd423282cba06f46efe9ee2"), "StudentName" : "Chris", "StudentOtherDetails" : [ { "StudentCountryName" : "US", "StudentSkills" : "MongoDB" }, { "StudentCountryName" : "UK", "StudentSkills" : "Java" } ] } -
MongoDB配列の要素を削除するにはどうすればよいですか?
要素を削除するには、MongoDBで$ pullを更新して、使用します。 $ pull演算子は、既存の配列から、指定された条件に一致する1つまたは複数の値のすべてのインスタンスを削除します。 まず、ドキュメントを使用してコレクションを作成しましょう- db.demo541.insertOne({"software":{"services":["gmail","facebook","yahoo"]}});{ "acknowledged" : true
-
MongoDBのJSON配列の内部要素にアクセスしますか?
MongoDBのJSON配列の内部要素にアクセスするには、ドット表記を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo687.insert({CountryName:'US', ... info: ... { ... id:101, ... details: ... [ ... { ... Name:'Chris', ... SubjectName:'MongoDB', ... otherDetails:{ ...