在mongoose中使用$ push in Array

时间:2014-12-14 11:42:30

标签: mongodb express mongoose

我的NetworkSchema中有一个数组,我想用mongoose中的 findOneandupdate 更新另一个条目

我的模特

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var NetworkSchema = new Schema({

UserID: {
    type: String,
    default: '',
    trim: true
},
NetworkList: [{
    Fid: {

        type: Schema.ObjectId,
        ref: 'User'
    }
}]});

HTML

<div ng-controller="AuthenticationController" style="margin-top: 50px">

<div ng-controller="NetworksController">
    <ul>

        <li ng-repeat="Network in Networkfriends">

            {{Network.firstName}}
            <button type="submit" class = "btn btn-default" data-ng-click="update(Network._id)">Add as a Friend</button>
        </li>
    </ul>
    <button type="submit" ng-click="showlist()">show</button>
</div>
</div>

网络控制器:

$scope.update = function(FriendID)
    {
        var UserID = this.authentication.user._id;
        console.log('User ID :' +UserID);
        var NetworkList = [{

            Fid: this.FriendID
        }];

        $http.post('/networks/update',{UserID: UserID, FriendID: FriendID}, $scope.credentials).success(function(response){
            console.log(JSON.stringify(response));

        });
}

Network.Server.Controller:

var mongoose = require('mongoose'),
    errorHandler = require('./errors.server.controller'),
    Network = mongoose.model('Network'),
    _ = require('lodash');
    util = require('util');

exports.update = function(req, res) {

var query={'UserID':req.body.UserID};


var NetworkList = [{Fid: req.body.FriendID}];
var update = {'NetworkList' :NetworkList};


Network.findOneAndUpdate(query,update,function(err){
    if (err) {
        return err;
    } else {
        NetworkList.push(req.body.FriendID);

    }
});
};

我想在 usersList 中添加用户的objectID,我在HTML页面中从NetworkFriends获取,如上所示,每当我点击添加为好友时按钮和我得到的是我的数据库中的一个新条目,其ObjectID为NetworkFriend,我希望通过添加为朋友按钮

分享多个NetworkFriend的逗号分隔对象ID

以下是我的数据库中的条目

{
"NetworkList" : [ 
    {
        "Fid" : ObjectId("5486fab40bc27314276be8cf"),
        "_id" : ObjectId("548d6ed5d6994cac219f536f")
    }
],
"UserID" : "547eaaab6c39471c3f5aebb6",
"__v" : 0,
"_id" : ObjectId("548adbc8b7eac44013bf188d")

}

我的目标是:

 {
"NetworkList" : [ 
    {
        "Fid" : ObjectId("5486fab40bc27314276be8cf"),"(objectID of another friend with comma separated like this"),
        "_id" : ObjectId("548d6ed5d6994cac219f536f")
    }
],
"UserID" : "547eaaab6c39471c3f5aebb6",
"__v" : 0,
"_id" : ObjectId("548adbc8b7eac44013bf188d")

}

1 个答案:

答案 0 :(得分:1)

您的目标不是一个有效的结构。你可能想要的是一个模式,其中NetworkList直接包含用户朋友的ObjectIds数组:

var NetworkSchema = new Schema({
    UserID: {
        type: String,
        default: '',
        trim: true
    },
    NetworkList: [{
        type: Schema.ObjectId,
        ref: 'User'
    }]
});

您的文档将如下所示:

{
    "_id" : ObjectId("548adbc8b7eac44013bf188d"),
    "NetworkList" : [ 
        ObjectId("5486fab40bc27314276be8cf") 
    ],
    "UserID" : "547eaaab6c39471c3f5aebb6",
    "__v" : 0
}

然后,您可以使用$push运算符向NetworkList添加新朋友,如下所示:

exports.update = function(req, res) {
    var query={'UserID': req.body.UserID};
    var update = {$push: {'NetworkList': req.body.FriendID}};

    Network.findOneAndUpdate(query, update, function(err, doc){ ... });
};