在 MongoDB 中更新巢狀在物件陣列中的字串陣列
讓我們建立一個包含文件的集合 -
> db.demo411.aggregate(
... [
... {$project : {
... _id : 0,
... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}}
... }
... }
... ]
... )
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] }
> db.demo412.insertOne(
... {
... "Information1" : [
... {
... "Information2" : [
... "John",
... "David"
... ]
... },
... {
... "Information2" : [
... "Mike"
... ]
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e70f38b15dc524f70227683")
}使用 find() 方法顯示來自集合的所有文件 -
> db.demo412.find();
這將生成以下輸出 -
{ "_id" : ObjectId("5e70f38b15dc524f70227683"), "Information1" : [ { "Information2" : [ "John", "David" ] }, { "Information2" : [ "Mike" ] } ] }以下是 MongoDB 中更新巢狀在物件陣列中的字串陣列的查詢 -
> db.demo412.updateMany(
... { _id: ObjectId("5e70f38b15dc524f70227683") },
... { $pull : {'Information1.$[].Information2' : "Mike" } }
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }使用 find() 方法顯示來自集合的所有文件 -
> db.demo412.find();
這將生成以下輸出 -
{ "_id" : ObjectId("5e70f38b15dc524f70227683"), "Information1" : [ { "Information2" : [ "John", "David" ] }, { "Information2" : [ ] } ] }
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP