简体   繁体   English

更新JSON数组(Mongoose)内的croncrete JSON对象中的特定字段

[英]Update a specific field in a croncrete JSON object inside an JSON array (Mongoose)

I am trying to update a specific field in a concrete JSON object inside an JSON array with mongoose. 我试图用mongoose更新JSON数组中的具体JSON对象中的特定字段。 My MongoDB contains: 我的MongoDB包含:

db.users.find({username:"xxx"})
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529yyy", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }

And I want to get the JSON object that matches with "username" : "yyy" and "user._id" = "56cb877e73da764818ec5ded" and change its token to "token" : "4529zzz" with mongoose, like this: 我想获得与"username" : "yyy"匹配的JSON对象"username" : "yyy""user._id" = "56cb877e73da764818ec5ded"并将其令牌更改为"token" : "4529zzz"使用"token" : "4529zzz" ,如下所示:

{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }

The schema of db is: db的架构是:

var userSchema = new Schema({
    username  : { type: String, required: true },
    ...

    githubtoken: [ { username: {type: String, required: false },
        token: {type: String, required: false}  }]

});

And update method: 并更新方法:

    userSchema.statics.updateuser = function updateuser (query, update, options) {
        var promise = new Hope.Promise();
        this.findAndUpdate(query, update, options,function(error, user) {
            if (error) {
                return promise.done(error, null);
            }else {
                return promise.done(null, user);
            }
        });
        return promise;
    };

And in my service with express.js: 在我的express.js服务中:

query =  {$and: [{_id: userid}, {githubtoken: {username:username, token:oldToken}} ]};
        update = {$set : {githubtoken:{username:username, token:token}}};

    options = { new: true};


    User.updateuser(query,update,options).then(function (error,user){
        if(error){
            return promise.done(error,null);
        }else{

}});

But it doesn't work, because remove all array of githubtokens and push only the new githubtoken, like this: 但它不起作用,因为删除所有githubtokens数组并只推送新的githubtoken,如下所示:

{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") }  }

Any idea? 任何想法? Thank you very much :D 非常感谢你:D

Try changing the query like this: 尝试像这样更改查询:

var query = {
    {
        _id: new ObjectId(userid),
        'githubtoken.username': username,
        'githubtoken.token': oldToken
    }
};

And update like this: 并像这样更新:

var update = {
    $set: {
        'githubtoken.$.username': username,
        'githubtoken.$.token': token
    }
};

You can use the $ - positional operator for the update: 您可以使用$ - 位置运算符进行更新:

db.collection('users').update({
    "_id": ObjectId("56cb877e73da764818ec5ded"),
    "githubtoken.username": "xxx"
}, {
    $set: {
        "githubtoken.$.username": username,
        "githubtoken.$.token": token
    }
});

And this should do the trick. 这应该可以解决问题。 More details on usage of $ - positional operator https://docs.mongodb.org/manual/reference/operator/update/positional/ 关于$ - positional operator https://docs.mongodb.org/manual/reference/operator/update/positional/的使用的更多细节

I found the solution in this page : 我在这个页面找到了解决方案:

query = {"githubtoken" :{$elemMatch: {"username": username}}};


            update = {$set : {
                "githubtoken.$.username": username,
                "githubtoken.$.token": token
            }};

Thanks for the help :D 谢谢你的帮助:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM