简体   繁体   English

Mongodb 3.2 Node js $ push不起作用

[英]Mongodb 3.2 Node js $push is not working

I want to push new friend JSON object to an array : friend I have try $push directly via mongodb but it is not working on NodeJS Anyone know want is going wrong? 我想将新的朋友JSON对象推送到数组:朋友我直接通过mongodb尝试了$ push,但是在NodeJS上不起作用任何人都知道想要出错了吗?

I have try UsersInfo.friend -> friend UsersInfo.friend.user_id -> user_id 我尝试了UsersInfo.friend-> friend UsersInfo.friend.user_id-> user_id

and no err return. 而且没有错误返回。

The Structure of Player : 播放器的结构: Mongo DB播放器结构

socket.on('follow_friend',function(data){
    var me = current_players_socket.indexOf(socket);
    console.log("PLayer id:"+data.user_id+" Player name:"+current_players[me].name+ "'s friend following request received");
    console.log("Target:"+data.fdid);
    MongoClient.connect(url,function(err,db){
        assert.equal(null,err);
        follow_friend_to_db(data,db,function(){
            db.close();
        });
    });
});

var follow_friend_to_db = function(data,db,callback){
    var me = current_players_socket.indexOf(socket);
    console.log("Following "+data.fdid+" to "+data.user_id);
            current_players[me].friend.push(data.fdid);
    db.collection('Users').update(
        {"UsersInfo.user_id":data.user_id},
        {
            $push: {
                "UsersInfo.friend" : 
                {$each:
                    {
                        "UsersInfo.friend.user_id" : data.fdid,
                        "UsersInfo.friend.add_date" : new Date("2014-10-01T00:00:00Z")
                    }
                }
            }
        },function(err,results){
            for(i= 0;i<current_players[me].friend.length;i++){
                console.log(current_players[me].friend[i]);
            }
            socket.emit('friend_followed',{fdid:data.fdid});
            callback();
        });
};

here is the $set function which is work 这是有效的$ set函数

    socket.on('gold_add',function(data){
    console.log("Player id:"+data.user_id+" add gold request recevied");
    var i = current_players_socket.indexOf(socket);

    MongoClient.connect(url,function(err,db){
        assert.equal(null,err);
        update_user_gold_to_db(data,db,function(){
            db.close();
        });
    });
    console.log("Player:"+current_players[i].get_name()+"'s gold:"+current_players[i].gold);

});

var update_user_gold_to_db = function(data,db,callback){
    var i = current_players_socket.indexOf(socket);
    console.log("Player id:"+data.user_id+" add gold amount:"+data.amount);
    var t = data.amount + current_players[i].get_gold();
    console.log(t);
    db.collection('Users').update(
        {"UsersInfo.user_id":data.user_id},
        {
            $set:{"UsersInfo.gold":t}
        }, function(err,results){
            //console.log(results);
            current_players[i].gold+=data.amount;
            socket.emit('gold_add_success',{gold:current_players[i].gold});
            callback();
        });
};

Why are you using $each. 为什么使用$ each。 We use $each when we have an array to push like mentioned in mongodb documentation: 当我们有一个要推送的数组(如mongodb文档中所述)时,我们使用$ each:

db.inventory.update(
   { _id: 2 },
   { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
 )

Try removing $each: 尝试删除$ each:

var follow_friend_to_db = function(data,db,callback){
    var me = current_players_socket.indexOf(socket);
    console.log("Following "+data.fdid+" to "+data.user_id);
            current_players[me].friend.push(data.fdid);
    db.collection('Users').update(
        {"UsersInfo.user_id":data.user_id},
        {
            $push: {
                "UsersInfo.friend" : 
                    {
                        "user_id" : data.fdid,
                        "add_date" : new Date("2014-10-01T00:00:00Z")
                    }
            }
        },function(err,results){
            for(i= 0;i<current_players[me].friend.length;i++){
                console.log(current_players[me].friend[i]);
            }
            socket.emit('friend_followed',{fdid:data.fdid});
            callback();
        });
};

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

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