Tag: mongoose

Mongoose findOneAndUpdate和runValidators不起作用

我有问题试图让'runValidators'选项工作。 我的用户模式有一个电子邮件字段,其中必须设置为true,但每次有新用户被添加到数据库(使用'upsert'选项),并且电子邮件字段为空时,不会抱怨: var userSchema = new mongoose.Schema({ facebookId: {type: Number, required: true}, activated: {type: Boolean, required: true, default: false}, email: {type: String, required: true} }); findOneAndUpdate代码: model.user.user.findOneAndUpdate( {facebookId: request.params.facebookId}, { $setOnInsert: { facebookId: request.params.facebookId, email: request.payload.email, } }, {upsert: true, new: true, runValidators: true, setDefaultsOnInsert: true }, function (err, user) { if (err) { console.log(err); […]

灰烬与节点(MEEN堆栈?)

我已经在Rails平台上构build了一年多的应用程序。 这就是说,如果你能够对Rails进行任何比较,它可能会帮助所有相关方。 只是一个想法。 无论如何,我试图围绕节点,前端框架,烬。 我最初对MEAN栈的绊脚石感兴趣。 深入挖掘之后,我意识到自己并不太喜欢angular色,并开始寻找替代品。 Ember看起来非常棒,语法看起来更干净,没有DOM的所有标记。 我想尝试mongoDB,我觉得这种方法会教我很多。 我想问的是Express。 Express在使用Node时有什么作用? 看来Ember(即使是Angular也是如此)有一个非常有performance力的路由器。 在我对这一切的有限理解中,我认为Express是为了路线? 我发现这似乎是一个MEEN堆栈。 MEAN的package.json是巨大的,但是我连接到的是非常小的,只是表示,mongoose和请求。 另一个问题,我想是在MEAN / MEEN堆栈中不同的angular色是什么? 那有意义吗? 我有点懒散,哈哈,但是我希望继续研究和玩这个东西,直到我有了这个“啊哈! 那一刻,你知道吗? mongoose – 似乎是一个更好的方式与mongoDB交互? express – 特定于节点的路由? 请求 – 似乎很简单。 将外部请求发送到networking上的其他页面。 我看不出什么相同的包裹会是在意味着,这是真正需要的? 咕噜咕噜咕噜咕噜咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕噜 任何解释,链接,或帮助,不胜感激。

与mongoose / node.js共享数据库连接参数的最佳方法

我正在使用Mongoose来pipe理Mongo数据库。 我的连接文件很简单: var mongoose = require('mongoose') mongoose.connection.on("open", function(){ console.log("Connection opened to mongodb at %s", config.db.uri) }); console.log("Connecting to %s", config.db.uri) mongoose.connect(config.db.uri) global.mongoose = mongoose 然后在我的app.js我只是 require('./database) 而“mongoose”variables在全球范围内可用。 我宁愿不使用全局variables(至less不是直接)。 有没有更好的方式通过节点(我使用express.js)通过单例模式或其他方法共享数据库连接variables?

在MongoDB中使用mongoose批量插入

有没有任何select与mongoose执行大量upserts? 所以基本上有一个数组,并插入每个元素,如果它不存在或更新它,如果它存在? (我正在使用海关_ids) 当我使用.insert MongoDB返回错误E11000重复的键(这应该更新)。 插入多个新文档虽然工作正常: var Users = self.db.collection('Users'); Users.insert(data, function(err){ if (err) { callback(err); } else { callback(null); } }); 使用.save返回一个参数必须是单个文档的错误: Users.save(data, function(err){ … } 这个答案表明没有这样的选项,但它是特定于C#,也已经3岁了。 所以我想知道是否有任何select使用mongoose做到这一点? 谢谢!

MissingSchemaError:架构尚未注册模型

我有一个Node.js – Express 3 – MongoDB的典型项目 我试图在我的/routes/index.js中对我的模型“Tweet”进行查询,当我运行我的应用程序时崩溃 24 Aug 11:35:07 – [nodemon] starting `node app.js` /Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286 throw new mongoose.Error.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model "Teewt". Use mongoose.model(name, schema) at Mongoose.model (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286:13) at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/routes/index.js:6:33) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) […]

摩卡beforeEach vs之前执行

最近我碰到一个我无法解释的问题。 在这些testing中我有很多代码,所以我会尽我所能来捕捉这个主意 我有这样的testing: describe('main page', function(){ beforeEach(function(done){ addUserToMongoDb(done); // #1 }); afterEach(function(done){ removeUserFromMongoDb(done); }); context('login', function(){ it('should log the user in, function(){ logUserIn(user_email); // #2 – This line requires the user from the beforeEach }); }); context('preferences', function(){ before(function(done){ //#3 logUserInBeforeTest(user_email); }); it('should show the preferences', function(){ doCheckPreferences(); // #4 }); }); }); 问题是, #1运行良好。 […]

Node.js错误错误:找不到模块'mongoose'

C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot>node bot.js node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot find module 'mongoose' at Function._resolveFilename (module.js:334:11) at Function._load (module.js:279:25) at Module.require (module.js:357:17) at require (module.js:368:17) at Object.<anonymous> (C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot\db.j s:1:78) at Module._compile (module.js:432:26) at Object..js (module.js:450:10) at Module.load (module.js:351:31) at Function._load (module.js:310:12) at Module.require (module.js:357:17) 我已经安装它使用npm安装,我试图重新安装,但没有任何想法?

Mongoose使用.select()方法

我很困惑与select方法的使用。 这是我如何使用它,这是错误的: Transaction.find({username : user.username}).select('uniqueId', 'confirmation_link', 'item_name', 'timeout', 'username', function(err, txs){ callback(txs); }); 我想要实现的只是从数据库中的事务中select那个用户名,我想只拿出select方法中列出的字段。 任何人都可以指出我应该如何使用select方法? 谢谢。

Mongoose创build模型时出错(使用步骤)

当试图在Mongoose中创build模型时,出现以下错误 [TypeError:无法读取未定义的属性'选项] 我不知道是什么原因造成的 "use strict"; var Step = require('step'); var mongoose = require('mongoose'); var Schema = mongoose.Schema; function randomFunction() { var categorySchema = new Schema({ id: Number, name: String, description: String }, { collection: 'categories' }); var Category; //… mongoose.connect('mongodb://localhost/grouping'); new Step( function() { //Connect to mongodb var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); […]

.save()和使用update()之间的Mongoose区别

要修改mongoose现有条目中的字段,使用什么区别 model = new Model([…]) model.field = 'new value'; model.save(); 和这个 Model.update({[…]}, {$set: {field: 'new value'}); 我问这个问题的原因是有人对我昨天发布的问题的build议: NodeJS和Mongo – 多个用户同时发送请求时的意外行为 。 该人build议使用更新,而不是保存,我还不完全确定为什么它会有所作为。 谢谢!