Tag: mongodb

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); […]

什么是正确的方式来处理Mongodb连接?

我使用10gen的native node.js驱动器一起尝试使用mongodb(2.2.2)的node.js。 起初一切顺利。 但是当来到并发基准testing部分时,出现了很多错误。 经常使用1000个并发连接/closures可能会导致mongodb拒绝任何错误的进一步请求,如: Error: failed to connect to [localhost:27017] Error: Could not locate any valid servers in initial seed list Error: no primary server found in set 另外,如果很多客户端没有明确closures就closures,那么需要花费mongodb分钟来检测并closures它们。 这也会导致类似的连接问题。 (使用/var/log/mongodb/mongodb.log来检查连接状态) 我已经尝试了很多。 根据手册,mongodb没有连接限制,但poolSize选项似乎对我没有任何影响。 因为我只在node-mongodb-native模块中使用过它,所以我不太确定最终导致问题的原因。 其他语言和驱动程序的性能呢? PS:目前,使用自我维护池是我想出的唯一解决scheme,但使用它不能解决与副本集的问题。 根据我的testing,副本集似乎没有多less独立的mongodb连接。 但不知道为什么会发生这种情况。 并发testing代码: var MongoClient = require('mongodb').MongoClient; var uri = "mongodb://192.168.0.123:27017,192.168.0.124:27017/test"; for (var i = 0; i < […]

与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运行良好。 […]

格式化来自Mongodb的ISODate

在Mongodb中,我以ISODate格式存储date和时间。 看起来像这样 ISODate("2012-07-14T01:00:00+01:00") 使用nodejs / javascript,我怎样才能显示时间组件,所以我会得到这样的东西 Time : 01:00 我正在使用momentjs使这更容易,但从我可以告诉momentjs似乎支持ISODate格式。 感谢您的帮助。

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创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议使用更新,而不是保存,我还不完全确定为什么它会有所作为。 谢谢!