2015-10-07 197 views
3

我按照預期在mongo shell上處理了以下查詢。

db.getCollection('personnels').update(
    { 
     _id: ObjectId("55f6728b9d73a15807885de8"), 
     "Devices._id":ObjectId("55fa5f7ac9e7863a3836e331") 
    }, 
    { 
     $pull:{ "Devices.$.DeviceCloudFolders": { "CloudFolderId": ObjectId("5615124b06275f072040c4f1")}} 
    } 
); 

這裏是我的文檔結構:

{ 
    "_id" : ObjectId("55f6728b9d73a15807885de8"), 
    "FirstName" : "Tolga", 
    "Devices" : [ 
     { 
      "_id" : ObjectId("55fa5f7ac9e7863a3836e331"), 
      "Name" : "tolga-laptop", 
      "DeviceCloudFolders" : [{ 
       "AuthorityType" : 1, 
       "CloudFolderId" : ObjectId("55f96db5c9e7863a3836e310"), 
       "Status" : 1 
      }], 
      "Status" : 1 
     } 
    ], 
    "Status" : 1 
} 

我需要在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); 

回答

5

我不知道,但你可以嘗試使用這樣的:

var update = Builders<Personnel>.Update.PullFilter(
    "Devices.$.DeviceCloudFolders", 
    Builders<DeviceCloudFolder>.Filter.Eq("CloudFolderId", _cloudFolderId)); 
+0

好了,我沒想到的是工作:)但由於。 –