node.js - Update array in mongodDB collection -
building api node/express. i'm having problem updating array inside collection mongodb. want add reference id useritems array in collection user.
user looks this:
[ { fbname: "name.name", email: "name.name@hotmail.com", username: "mr.name", useritems: [objectid("51e101df2914931e7f000003"), objectid("51e101df2914931e7f000005"), objectid("51cd42ba9007d30000000001")] }];
in server.js:
var express = require('express'), item = require('./routes/items'); var app = express(); app.configure(function () { app.use(express.logger('dev')); app.use(express.bodyparser()); }); app.put('/users/:userid/item/:itemid/add', item.adduseritem);
query:
exports.adduseritem = function(req, res) { var user = req.params.userid; var item = req.params.itemid; console.log('updating useritem user: ' + user); console.log("item: ", item); db.collection('users', function(err, collection) { collection.update({'_id':new bson.objectid(user)}, {$addtoset: { useritems: item}}); }); }
when try curl nothing happens , error: (52) empty reply server, curl
curl:
curl -i -x put -h 'content-type: applicatio/json' http://127.0.0.1:3000/users/51e6a1116074a10c9c000007/item/51e6a1116074a10c9c000006/add
try this:
collection.update({'_id':new bson.objectid(user)}, {$addtoset: { useritems: item}}, true, function(err, result){ res.json(200, result); });
Comments
Post a Comment