如何使用MongoDB查询取消设置特定对象

时间:2019-05-02 09:45:54

标签: mongodb mongodb-query

这里我有一个名为find的数组,该数组中的值与我的文档 hobbys 中存在的值相同,我想删除特定的对象,但是我尝试了做不到,请帮助我。

  

“我的爱好”文档

 {
    "_id" : ObjectId("5ccaa152feee5f2e60dff06f"),
    "name" : "ABC",
    "hobbies" : {
        "cricket" : {
            "hobbyName" : "cricket"
        },
        "football" : {
            "hobbyName" : "football"
        }
    }
},
{
    "_id" : ObjectId("5ccaa196feee5f2e60dff070"),
    "name" : "D",
    "hobbies" : {
        "Tennis" : {
            "hobbyName" : "Tennis"
        },
        "Volleyball" : {
            "hobbyName" : "Volleyball"
        },
        "basketball" : {
            "hobbyName" : "basketball"
        }
    }
}
  

取消设置我的文档后,请在下面查看此内容(期望输出)

{
    "_id" : ObjectId("5ccaa152feee5f2e60dff06f"),
    "name" : "ABC",
    "hobbies" : {
        "cricket" : {
            "hobbyName" : "cricket"
        },
        "football" : {
            "hobbyName" : "football"
        }
    }
},
{
    "_id" : ObjectId("5ccaa196feee5f2e60dff070"),
    "name" : "D",
    "hobbies" : {
        "basketball" : {
            "hobbyName" : "Volleyball"
        }
    }
}
  

我已经尝试过了

let find = [ 'Tennis', 'basketball' ];
let match = {'$or' : [] };
for(let i=0;i<find.length;i++){ 
   let key1  = find[i];
   let obj = {};
   obj[find[i]] = { $exists : true };
   match['$or'].push(obj);
}
print(match);
db.Hobbies.find(match)

0 个答案:

没有答案