我按照预期在mongo shell上运行以下查询.
db.getCollection('personnels').update(
{
_id: ObjectId("55f6728b9d73a15807885de8"),
"Devices._id":ObjectId("55fa5f7ac9e7863a3836e331")
},
{
$pull:{ "Devices.$.DeviceCloudFolders": { "CloudFolderId": ObjectId("5615124b06275f072040c4f1")}}
}
);
Run Code Online (Sandbox Code Playgroud)
这是我的文档结构:
{
"_id" : ObjectId("55f6728b9d73a15807885de8"),
"FirstName" : "Tolga",
"Devices" : [
{
"_id" : ObjectId("55fa5f7ac9e7863a3836e331"),
"Name" : "tolga-laptop",
"DeviceCloudFolders" : [{
"AuthorityType" : 1,
"CloudFolderId" : ObjectId("55f96db5c9e7863a3836e310"),
"Status" : 1
}],
"Status" : 1
}
],
"Status" : 1
}
Run Code Online (Sandbox Code Playgroud)
我需要在C#中使用它并且无法弄清楚如何使用它.
我从这些线开始:
var filter = Builders<Personnel>.Filter.And(
Builders<Personnel>.Filter.Eq("_id", ownerPersonnelId),
Builders<Personnel>.Filter.Eq("Devices._id", _id));
var update = Builders<Personnel>.Update.PullFilter("Devices.$.DeviceCloudFolders", /*couldn't figure out what goes here*/))
Personnels.FindOneAndUpdateAsync(filter, update);
Run Code Online (Sandbox Code Playgroud) 我在MongoDB中有一个看起来像这样的文档:
{
"Id":"123",
"Product": "test",
"Tags":[
{
"Name": "name",
"Categories": [
{
//item
},
{
//item
}
]
},
{
"Name": "name",
"Categories": [
{
//item
},
{
//item
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要添加一个新的Item,它需要被添加到所有的类别Tags的该元素Product由它Id。例如,当我插入Item 3文档时,应如下所示:
{
"Id":"123",
"Product": "test",
"Tags":[
{
"Name": "name",
"Categories": [
{
//item 1
},
{
//item 2
},
{
//item 3
}
]
},
{
"Name": "name",
"Categories": [
{
//item 1
},
{ …Run Code Online (Sandbox Code Playgroud)