在mongoDb中,如何删除包含特定键和值的数组中的对象?

时间:2014-07-20 07:32:54

标签: mongodb pull

我试图从包含特定主题的教师数组中删除该字段,例如"ok baby"

{
"_id" : "billy",
"password" : "$2a$10$MKZFNtMhts6rMbnIoqXB9.Q8NHAizQAGhX5S6g.8zeRt7TpRpuQea",
"teacher" : [
    {
        "subject" : "ok baby",
        "students" : [
            "billy"
        ]
    },
    {
        "subject" : "adsfqewr",
        "students" : [
            "billy"
        ]
    }
]
}

这就是我的尝试:

users.update(      { 'teacher.subject':title, '_id':username},
                   { $pull: { 'teacher.subject':title } },
                   { multi: true }
)

1 个答案:

答案 0 :(得分:1)

查询应该是这样的。,,,从数组中提取数据是老师,标题等于标题......

users.update( { 'teacher.subject':title, '_id':username},
               { $pull: { 'teacher':{'subject':title}} },
               { multi: true }
 );