mongoDB updateOne不更新记录 [英] mongoDB updateOne not updating a record

查看:950
本文介绍了mongoDB updateOne不更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用updateOne在mongoDB中更新一条记录,取消更新的记录在数组中,并且所有字段都在更新,除了一个为 country 的字段,我尝试使用MongoChef更新它(用于Linux的MongoDB的GUI),但是它不起作用,如果我在GUI中使用编辑"来更新一个文档,那么该记录也可以在此之后进行更新.

I am trying to update a record in mongoDB using updateOne, the un updating record is in array, and all fields are updating except one field which is country, I have tried to update it using MongoChef (a GUI of MongoDB for linux), but it doesn't work, also If I update one the Document using Edit in GUI then that record is ready to update after that.

我在MongoChef中尝试了以下查询

I have tried with the following query in MongoChef

db.institutions.updateOne({
    "campus": { "$elemMatch": { "_id": ObjectId("578500ef87e4c326183e520e")} },
    "_id": ObjectId("57f25706762c06cb7d9422fc") 
    },
    {
    "$set" : { "campus.$.country" : "SS1"
    }
});

仅国家/地区字段未更新.如果我更新其他任何字段,都可以正常工作.

Only country field is not updating If I update any other field it works fine.

文档结构在

{ 
    "_id" : ObjectId("57f26824762c06cb7d982e37"), 
    "campus" : [
        {
            "_id" : ObjectId("578500ee87e4c326183e5201"), 
            "country" : "GB", 
            "coreId" : NumberInt(1), 
            "city" : "Norwich", 
        }
    ]
}

在此先感谢您的帮助

推荐答案

尝试此查询

db.institutions.updateOne(
    {
      "campus._id": ObjectId("578500ef87e4c326183e520e"),
      "_id": ObjectId("57f25706762c06cb7d9422fc") 
    },
    {
      "$set" : { "campus.$.country" : "SS1"}
    }
);

NB:如果使用mongodb驱动程序或猫鼬,则无需使用ObjectId(""),只需使用"578500ef87e4c326183e520e"

N.B: if use mongodb driver or mongoose then no need to use ObjectId("") just use "578500ef87e4c326183e520e"

这篇关于mongoDB updateOne不更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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