如何在mongoDB中结合使用$ unset和$ set [英] How to use $unset and $set in combination in mongoDB

查看:914
本文介绍了如何在mongoDB中结合使用$ unset和$ set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的记录:

{
    "Date" : ISODate("2013-06-28T18:30:00Z"),
    "Details" : {
            "Amount1" : -200,
            "Amount2" : 2800,
            "Amount3" : -100
    },
    'NID' : 'T123RT',
    'PID' : 'P123RT',
    "SettAmount" : 2500,
    "SettStatus" : "completed",
    "Status" : "completed",
    "StoreID" : "51ea54279d867b040b000008",
    "_id" : ObjectId("51ea54279d867b040b000013")
}

我正在尝试像这样更新文档:

I am trying to update the document like :

db.settlements.update({
    'StoreID' : "51ea54279d867b040b000008",
    'Date' : ISODate("2013-06-28T18:30:00Z")
}, {
    $unset : {
        'NID' : "",
        'PID' : ""
    }
    }, {
    $set : {
        'SettStatus' : 'start',
        'Status' : 'pending'
    }
});

但是,只有未设置的操作才能成功.上面的查询中有什么错误........?

But, only unset operation is successful. what is the error in above query........?

推荐答案

您的括号过多, 这是正确的命令:

you have too many braces, here's correct command:

db.settlements.update(
    {
        'StoreID': "51ea54279d867b040b000008",
        'Date': ISODate("2013-06-28T18:30:00Z")
    }, 
    {
        $unset: {
            'NID' : "",
            'PID' : ""
        }, 
        $set: {
            'SettStatus': 'start',
            'Status': 'pending'
        }
    }
);

在您的命令中,您在$ update命令中将$ set用作<options>,而不是<update>

in your command, you're using $set as <options> in update command, not as part of <update>

http://docs.mongodb.org/manual/core /update/#crud-update-update

这篇关于如何在mongoDB中结合使用$ unset和$ set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆