Mongodb 3.2节点js $ push无效

时间:2016-09-29 08:28:18

标签: javascript node.js mongodb

我想将新朋友的JSON对象推送到数组:朋友 我已经通过mongodb直接尝试$ push但它不能在NodeJS上运行 任何人都知道想要出错?

我试过UsersInfo.friend - >朋友UsersInfo.friend.user_id - > USER_ID

并且没有错误的回报。

玩家结构:Mongo DB Player Structure

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();
        });
};

这是$ 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();
        });
};

1 个答案:

答案 0 :(得分:0)

你为什么每人都使用$。当我们有像mongodb文档中提到的数组推送时,我们使用$:

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

尝试删除$ 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();
        });
};