使用Node.js操作Mongoose / MongoDB数组(Manipulating Mongoose/MongoDB Array using Node.js)
我注意到有关如何使用Mongoosejs操纵对象数组的文档和信息很少。
我有一个用户的以下模型/架构:
'use strict'; /** * Module Dependencies */ var bcrypt = require('bcrypt-nodejs'); var crypto = require('crypto'); var mongoose = require('mongoose'); /** * Custom types */ var ObjectId = mongoose.Schema.Types.ObjectId; var userSchema = new mongoose.Schema({ email: { type: String, unique: true, index: true }, password: { type: String }, type: { type: String, default: 'user' }, facebook: { type: String, unique: true, sparse: true }, twitter: { type: String, unique: true, sparse: true }, google: { type: String, unique: true, sparse: true }, github: { type: String, unique: true, sparse: true }, tokens: Array, profile: { name: { type: String, default: '' }, gender: { type: String, default: '' }, location: { type: String, default: '' }, website: { type: String, default: '' }, picture: { type: String, default: '' }, phone: { work: { type: String, default: '' }, home: { type: String, default: '' }, mobile: { type: String, default: '' } } }, activity: { date_established: { type: Date, default: Date.now }, last_logon: { type: Date, default: Date.now }, last_updated: { type: Date } }, resetPasswordToken: { type: String }, resetPasswordExpires: { type: Date }, verified: { type: Boolean, default: true }, verifyToken: { type: String }, enhancedSecurity: { enabled: { type: Boolean, default: false }, type: { type: String }, // sms or totp token: { type: String }, period: { type: Number }, sms: { type: String }, smsExpires: { type: Date } }, friends: [{ friend: { type: ObjectId, ref: 'User' }, verified: { type: Boolean, default: false } }] }); /* (...) some functions that aren't necessary to be shown here */ module.exports = mongoose.model('User', userSchema);
因此,您可以检查我在用户中定义的朋友,如下所示:
friends: [{ friend: { type: ObjectId, ref: 'User' }, verified: { type: Boolean, default: false } }]
现在的问题是如何在Node.js脚本中添加,编辑和删除此数组?
BOTTOMLINE:如何使用Node.js和Mongoose.js操作MongoDB Schema中的数组? 我是否总是要创建一个Schema功能,还是可以直接访问它?
编辑(2014年7月13日):到目前为止,我已经创建了一个HTTP GET,它给了我这样的数组:
app.get('/workspace/friends/:userid', passportConf.isAuthenticated, function (req, res) { User.find({_id: req.params.userid}, function (err, items) { if (err) { return (err, null); } console.log(items[0].friends); res.json(items[0].friends); }); });
但这只返回一个friendIds数组,但是如果我想创建某种'/ workspace / friends /:userid / del /:friendid'POST或添加POST会怎样。 我似乎无法弄清楚如何完成这项工作。
I've noticed there's little documentation and info about how I should manipulate an array of objects using Mongoosejs.
I have the following model/Schema for an User:
'use strict'; /** * Module Dependencies */ var bcrypt = require('bcrypt-nodejs'); var crypto = require('crypto'); var mongoose = require('mongoose'); /** * Custom types */ var ObjectId = mongoose.Schema.Types.ObjectId; var userSchema = new mongoose.Schema({ email: { type: String, unique: true, index: true }, password: { type: String }, type: { type: String, default: 'user' }, facebook: { type: String, unique: true, sparse: true }, twitter: { type: String, unique: true, sparse: true }, google: { type: String, unique: true, sparse: true }, github: { type: String, unique: true, sparse: true }, tokens: Array, profile: { name: { type: String, default: '' }, gender: { type: String, default: '' }, location: { type: String, default: '' }, website: { type: String, default: '' }, picture: { type: String, default: '' }, phone: { work: { type: String, default: '' }, home: { type: String, default: '' }, mobile: { type: String, default: '' } } }, activity: { date_established: { type: Date, default: Date.now }, last_logon: { type: Date, default: Date.now }, last_updated: { type: Date } }, resetPasswordToken: { type: String }, resetPasswordExpires: { type: Date }, verified: { type: Boolean, default: true }, verifyToken: { type: String }, enhancedSecurity: { enabled: { type: Boolean, default: false }, type: { type: String }, // sms or totp token: { type: String }, period: { type: Number }, sms: { type: String }, smsExpires: { type: Date } }, friends: [{ friend: { type: ObjectId, ref: 'User' }, verified: { type: Boolean, default: false } }] }); /* (...) some functions that aren't necessary to be shown here */ module.exports = mongoose.model('User', userSchema);
So as you can check I defined Friends inside User like this:
friends: [{ friend: { type: ObjectId, ref: 'User' }, verified: { type: Boolean, default: false } }]
Now the question is how can I add, edit and delete this array in a Node.js script?
BOTTOMLINE: How can I manipulate arrays that are inside MongoDB Schemas, using Node.js and Mongoose.js? Do I always have to create a Schema function or can I access it directly?
EDIT (13/07/2014): So far I've created a HTTP GET that gives me the array like this:
app.get('/workspace/friends/:userid', passportConf.isAuthenticated, function (req, res) { User.find({_id: req.params.userid}, function (err, items) { if (err) { return (err, null); } console.log(items[0].friends); res.json(items[0].friends); }); });
But this only returns an array of friendIds, but what if I want to create some sort of '/workspace/friends/:userid/del/:friendid' POST, or add POST. I can't seem to figure out how I can get this done.