Tag: mongoose

如何做嵌套数组文件的mongoose聚合

我有一个Mongodb集合, 轮询与以下架构 { "options" : [ { "_id" : Object Id, "option" : String, "votes" : [ Object Id ] // object ids of users who voted },….. ] } 假设我有我想要发送这个信息的节点js中的用户的userId 。 我的任务是 (1)在上面的json对象(我​​使用mongoose)中包含一个额外的字段。 如 “myVote”:option._id 我需要findoption._id为其中 选项[someIndex] .votes包含userId (2)改变每个选项中现有的“票数”字段以表示特定选项的票数,如示例中所见 例: { "options" : [ { "_id" : 1, "option" : "A", "votes" : [ […]

如何在MongoDB中立即保存对象?

在我的Node应用程序中,我使用mongoose模块。 我发现之后 model_name.create({…}) 对象不是立即创build,而是有一些延迟(缓冲区?)。 我该如何强制在这个特定时刻保存这个新对象呢?

Mongo / Mongoose – 通过部分文档查找

我有一组属性,都有地址。 "address" : { "street" : "5 Orange Drive", "city" : "Orlando", "state" : { "abbreviation" : "FL", "name" : "Florida" }, "zip" : "32822", "geo" : { "lat" : 28.519, "lng" : -81.304 } }, "address" : { "street" : "16 Main Street", "city" : "Tallahassee", "state" : { "abbreviation" : "FL", "name" : […]

从表更新数据到mongodb

是否有任何框架或技术来简单地更新表中编辑和存储在mongoDB中的数据 我正在使用angularjs,node.js和mongoDB。 我“手工”的想法是: 跟踪angular表中的编辑,它将返回带有更新字段的json 用适当的文档ID发送更新编辑字段的REST请求

节点mongoose总是在findOne上返回错误

我使用passport.js来login用户,每当我的本地身份validationfunction到达User.findOne(),它总是返回一个错误。 我不知道我在做什么错… 我的护照代码与findOne()调用: passport.serializeUser(function(user, done) { console.log('serialize user occurred'); done(null, user.id); }); // used to deserialize the user passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); }); passport.use('local-signup', new LocalStrategy({ // by default, local strategy uses username and password, we will override with email usernameField : 'email', passwordField : 'password' }, function(email, password, […]

将非结构化数据保存在mongoose中

我能够保存请求数据,如果我明确定义我的expression模型的结构,但我不能保存logging,如果我没有明确定义的数据结构。 例如,我可以保存,如果我在我的模型中有这个 …. module.exports = mongoose.model('Form', new Schema({ name: String, password: String, admin: Boolean })); … … 但是如果我这样做的话,我不能保存它 module.exports = mongoose.model('Form', new Schema()); 这是我的模型 // get an instance of mongoose and mongoose.Schema var mongoose = require('mongoose'); var Schema = mongoose.Schema; // set up a mongoose model and pass it using module.exports module.exports = mongoose.model('Form', new […]

为什么在mongoose的预存储钩子和保存后钩子的“this”关键字不同?

这是我的代码的相关片段 MySchema .pre('save', function (next) { var self = this; console.log(self); return next(); }); MySchema .post('save', function (next) { var self = this; console.log(self); }); 出于某种原因,在这种情况下,预保存钩子给出了一个适当的对象 { farm: 557ce790a893e4e0118059e3, _id: 557ce791a893e4e011805a35, privileges: [ { instanceId: 557ce790a893e4e0118059bb, access: 5, modelType: 'User' } ], public: 0, properties: { crop: 'No Crop', name: 'Pirani Tract 50' }, geometry: […]

将数组与包含对象的数组进行比较

这是我的集合架构: var objectSchema = new Schema({ members: [{ user_id: ObjectId, settings: { type: Boolean } }], title: String }); 现在我试图search具有特定成员的对象(由其“user_id”标识,例如[“asdf123lkd”,“asdf1223”])。 有什么方法可以search这些对象吗? 谢谢!

多列独特的mongoose节点的组合

目的 为两列创build唯一性 我试过了 这是我的模式, var mongoose = require('mongoose'); // location table Schema var locationSchema = new mongoose.Schema({ locationId: { type: String, required: true }, stockingLocationId: { type: String, required: true}, parentStockingLocationId: { type: String }, stockingLocationDescription: { type: String }, created: { type: Date, default: Date.now }, lastModified: { type: Date, default: Date.now }, isActive: […]

在post hook中间填充mongoose中的'find'

我有用户发布在我的网站上的文章的文章架构。 它引用了用户集合: var ArticleSchema = new Schema({ title: { // NO MARKDOWN FOR THIS, just straight up text for separating from content type: String, required: true }, author: { type: Schema.Types.ObjectId, ref: 'User' } }); 我想在所有find / findOne调用上有一个post钩子来填充引用: ArticleSchema.post('find', function (doc) { doc.populate('author'); }); 出于某种原因,在钩子中返回的文档没有填充方法。 我是否必须使用ArticleSchema对象而不是文档级别填充?