Tag: mongoose

使用mongoose为mongo对象设置字段为空

我在一个对象上调用user.save(),我设置user.signup_date = null; user.first_name = null; user.signup_date = null; user.save(); 但是,当我看着MongoDB中的用户仍然有signup_date和first_name设置…我如何有效地设置此字段为空或空?

mongoose填充子子文档

我在我的MongoDB中有这个设置 项目: title: String comments: [] // of objectId's 注释: user: ObjectId() item: ObjectId() comment: String 这是我的Mongoose模式: itemSchema = mongoose.Schema({ title: String, comments: [{ type: Schema.Types.ObjectId, ref: 'comments' }], }); Item = mongoose.model('items', itemSchema); commentSchema = mongoose.Schema({ comment: String, user: { type: Schema.Types.ObjectId, ref: 'users' }, }); Comment = mongoose.model('comments', commentSchema); 这是我得到我的项目与评论的地方: Item.find({}).populate('comments').exec(function(err, data){ if […]

MongoDB mLab mongoose Node.js驱动程序 – 连接超时一段空闲时间后?

我有一个简单的Node.js ,使用mongoose与mLab上托pipe的Mongo数据库连接。 一切似乎工作得很好:添加新的logging,查询现有的东西。 有时候,经过一段时间的不活动之后,当我看着控制台时,我看到以下内容: events.js:160 throw er; // Unhandled 'error' event ^ Error: connection timeout at Db.<anonymous> (___PATH___/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:168:17) at emitTwo (events.js:106:13) at Db.emit (events.js:191:7) at Server.listener (___PATH___/node_modules/mongodb/lib/db.js:1786:14) at emitOne (events.js:96:13) at Server.emit (events.js:188:7) at Server.<anonymous> (___PATH___/node_modules/mongodb/lib/server.js:274:14) at emitOne (events.js:96:13) at Server.emit (events.js:188:7) at Pool.<anonymous> (___PATH___/node_modules/mongodb-core/lib/topologies/server.js:334:12) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection.<anonymous> (___PATH___/node_modules/mongodb-core/lib/connection/pool.js:270:12) at […]

模块化编程和节点

更新1 :我在这一方面取得了很多进展。 我几乎放弃了(至less现在,但也许长期)允许用户上传模块的想法。 但是,我正在开发一个结构,以便可以定义和加载几个模块。 一个模块将被初始化,设置自己的路由,并有一个“公共”目录来提供Javascript服务。我越发现,我越发意识到我可以(应该)也移动现在系统范围内的调用在一个叫做“系统”的模块中。 更新2 :我已经在这方面取得了巨大的进展。 我即将在GitHub上提交大量的代码,这将允许人们使用Node和Express来完成非常好的模块化编程(模块公开了客户端和服务器端代码)。 敬请期待。 更新3 :我重写这个东西作为一个系统来注册模块,并使他们能够通过事件/钩子系统进行通信。 这是非常好的。 我有很多代码已经很好了 – 我只是把它移植到新的系统上。 请随时查看GitHub上的项目: https : //github.com/mercmobily/hotplate ) 更新4 :这是很好的。 事实certificate,我关于一个模块是客户端和服务器的想法是真正的工作。 更新5 :模块越来越接近可用的东西。 我实现了一个新的loader,它将考虑init()函数将调用的所有()函数,并确保提供该钩子的模块将被首先加载。 这开辟了一个全新的水平。 更新6 :烤盘现在接近12000行代码。 到二月份的某个时候,我想可能会接近20000行代码。 它做了很多东西,这一切都从StackOverflow开始! 我需要它来开发我自己的SaaS,所以我真的需要在二月份之前完成它(这样我可以冲刺到七月份,完成第一个版本的BookingDojo)。 谢谢大家! 我写的东西可能会变成一个相当大的软件。 简单的说就是nodejs + Express + Mongodb / Mongoose + Dojo(客户端)。 注:本文中的问题标记为[Q1],[Q2]等 来自Drupal的背景(并知道它是如何coooomplex进化的,我想避免的东西),我有点模块怪胎。 目前,我已经完成了应用程序的样板(hotplate: https : //github.com/mercmobily/hotplate )。 它做所有的无聊的东西(用户,工作区,密码提醒等),它缺less了几件。 我想提出一个devise,将允许与Drupal(但可能更好)类似的模块。 那是: 模块可以定义新的路线,并处理它们 模块安装在系统范围内,然后每个工作区都可以启用它们的集合列表 […]

根据坐标和最大距离查找某个点的最近点 – 使用带有MEAN堆栈的Mongoose查询结果未定义

我有一个问题,我不能解决在一些日子,甚至看相关的堆栈溢出Q / A。 我正在开发一个应用程序,使用苏格兰人艾哈迈德·哈克(Ahmed Haque)的方法创build一个MEAN Stack Google Map应用程序教程 。 我试图实现一个应用程序,使用Google Maps API来绘制坐标包含在存储在MongoDB实例中的GeoJson文件中的Points , LineStrings和Polygons 。 我正在使用Mongoose为我的数据构buildS​​chema并查询我的MongoDB数据库。 我想find最近的点CP给定P0给定P0's latitude and longitude ,给出一个最大的半径distance用于find感兴趣的点。 给定图像结束后,我想,例如,如果我插入2000(公里),我的查询将find所有的点距离P0最远2000公里。 在这个例子中,它可能应该给我P1和P2。 当我在Mongoose Schema只有Point的时候,我可以做到这一点。 我只有标记(点)有这个Schema : // Pulls Mongoose dependency for creating schemas var mongoose = require('mongoose'); var Schema = mongoose.Schema; // Creates a User Schema. var MarkerSchema = new Schema({ username: {type: String, […]

我如何在Mongoose中创build/查找?

有时我需要在数据库中存在一个文档,并且很乐意使用现有的文档,或者在丢失文档的情况下创build文档,并使用新的文档。 这似乎是一个相当常见的用例,但我已经通过Mongoose文档看 ,我找不到任何东西。 像findOneAndUpdate()和update()和upsert: true这样的Mongoose Collection方法是关于修改文档的 – 我不希望修改文档(如果存在的话),只是获取对它的引用。 示例 :(为@neillunn添加)我想添加一个用户,引用一个名字为“foo”的公司。 在此之前,我想用{name: 'foo'}来查找一个公司,如果它不存在,就创build它。 示例2 :(为@neillunn添加)我现在用来处理示例场景的代码: // Find or create an an instance and return a cb with a reference to it. var findOrCreate = function(model, criteria, cb){ model.update(criteria, criteria, {upsert: true}, function(err, numberAffected, raw){ if ( ! raw.updatedExisting ) { console.log('Created instance') } else { console.log('Found […]

用mongoose更新嵌套对象

我已经search了很多关于嵌套对象的问题,但是我find了与array [s]相关的所有内容。 我正在寻找一个更新简单的mongoose嵌套对象。 从这里http://mongoosejs.com/docs/guide.html there is an example schema : var blogSchema = new Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }], date: { type: Date, default: Date.now }, hidden: Boolean, meta: { votes: Number, favs: Number } }); 一旦创build一个文件, 我以后怎么改变收益数字? 没有我能find的相同的文件。 这就是我所做的: blog.findById(entityId, function(err, mainDoc){ if(err || !mainDoc) […]

Mongoose模式/模型中的自定义构造函数

问候一切! 我如下定义了一个Mongoose模式并注册了一个模型(InventoryItemModel)。 有没有办法为模式创build一个自定义的构造函数,所以当我从模型实例化一个对象,函数将被调用(例如,从数据库加载对象的值)? var mongoose = require('mongoose') , Schema = mongoose.Schema var InventoryItemSchema = new Schema({ Sku : String , Quanity : Number , Description : String , Carted : [] , CreatedDate : {type : Date, default : Date.now} , ModifiedDate : {type : Date, default : Date.now} }); mongoose.model('InventoryItem', InventoryItemSchema); var item = […]

mongoose保存vs插入vs创build

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var notificationsSchema = mongoose.Schema({ "datetime" : { type: Date, default: Date.now }, "ownerId":{ type:String }, "customerId" : { type:String }, "title" : { type:String }, "message" : { type:String } }); var notifications = module.exports = mongoose.model('notifications', notificationsSchema); module.exports.saveNotification = function(notificationObj, callback){ //notifications.insert(notificationObj); won't work //notifications.save(notificationObj); won't […]

在所有使用摩卡的testing之后,在哪里删除数据库并closures连接

我试图找出到哪里放置一个函数来删除数据库,并在所有testing运行后closures连接。 这里是我的嵌套testing: //db.connection.db.dropDatabase(); //db.connection.close(); describe('User', function(){ beforeEach(function(done){ }); after(function(done){ }); describe('#save()', function(){ beforeEach(function(done){ }); it('should have username property', function(done){ user.save(function(err, user){ done(); }); }); // now try a negative test it('should not save if username is not present', function(done){ user.save(function(err, user){ done(); }); }); }); describe('#find()', function(){ beforeEach(function(done){ user.save(function(err, user){ done(); }); }); it('should find […]