Tag: mongodb

“mongoose”如何处理添加具有__NOT__模式部分的FIELDS的文档?

我正在玩mongoose快速入门指南。 http://mongoosejs.com/docs/index.html 我认为当我保存了一个文档中没有在模式中定义的字段时会引发错误。 相反,它在集合中创build了一个新的文档,但没有这个字段。 (注:我意识到mongodb本身是“无模式”的,因此集合中的每个文档都可以完全不同)。 两个问题 mongoose如何处理添加不属于模式一部分的字段的文档? 它似乎只是忽略它们,如果没有任何字段映射,只会用ObjectId创build一个空文档。 如果即使文档成功保存,文档的特定字段还没有被添加,您如何获得mongoose来警告您? (问题是 – 我相信 – 很简单,所以我没有添加代码,但如果有人请求,我肯定会。 谢谢。

mongoose是否同时允许多个数据库请求?

我读了Mongoose将只能打开一个连接最多每个集合,没有选项来改变这一点。 这是否意味着一个缓慢的mongo查询将使所有后续的查询等待? 我知道node.js中的所有内容都是非阻塞的,但我想知道一个缓慢的查询是否会延迟所有后续查询的执行。 而且是否有办法改变这一点。

如何在Mongoose中设置_id到db文件?

我试图通过计算数据库中的文档,并使用该数字来创build_id(假设第一个_id为0),dynamic地为我的Mongoose模型创build_id。 但是,我无法从我的价值观中获得_id。 这是我的代码: //Schemas var Post = new mongoose.Schema({ //_id: Number, title: String, content: String, tags: [ String ] }); var count = 16; //Models var PostModel = mongoose.model( 'Post', Post ); app.post( '/', function( request, response ) { var post = new PostModel({ _id: count, title: request.body.title, content: request.body.content, tags: request.body.tags }); post.save( function( […]

用于MongoDB的Node.js模块

在以下链接中有几个可用于Node.js的MongoDB模块 – https://github.com/joyent/node/wiki/modules#wiki-db-nosql-mongo 请给我一个build议(我想知道你为什么select那个?)。

Mongoose – RangeError:最大调用堆栈大小超出

我试图批量插入文件到MongoDB(所以绕过Mongoose和使用本地驱动程序,而不是Mongoose不支持批量插入文档数组)。 我这样做的原因是为了提高写作速度。 我在下面的代码中的console.log(err)处收到错误“RangeError:Maximum Call Stack Size Exceeded” function _fillResponses(globalSurvey, optionsToSelectRegular, optionsToSelectPiped, responseIds, callback) { Response.find({'_id': {$in: responseIds}}).exec(function(err, responses) { if (err) { return callback(err); } if (globalSurvey.questions.length) { responses.forEach(function(response) { console.log("Filling response: " + response._id); response.answers = []; globalAnswers = {}; globalSurvey.questions.forEach(function(question) { ans = _getAnswer(question, optionsToSelectRegular, optionsToSelectPiped, response); globalAnswers[question._id] = ans; response.answers.push(ans); }); }); […]

使用mongoose在MongoDB中设置集合的到期时间

以下是可以通过mongoterminal设置集合(TTL)到期时间的命令: db.log.events.ensureIndex( { "status": 1 }, { expireAfterSeconds: 3600 } ) 我如何从Node.js中的代码使用mongoose做到这一点?

基于Mongoose的应用程序体系结构

这不是一个具体的应用程序/代码问题,只是关于通用的应用程序架构。 我试图理解正确的方式来组织我的mongoose应用程序。 因为我是mongoose新手,现在就是这样做的: 核心/ settings.js var mongoose = require('mongoose'); exports.mongoose = mongoose; mongoose.connect('mongodb://localhost/blog'); exports.db = mongoose.connection; 核心/ models.js settings = require("./settings"); // post schema var postSchema = settings.mongoose.Schema({ header: String, author: String, text: String }) //compiling our schema into a Model exports.post = settings.mongoose.model('post', postSchema) 芯/ DB-layer.js settings = require("./core/settings"); models = require("./core/models"); exports.function = […]

我如何在Mongoose中执行一个ID数组查询?

比方说,我有一个名为User的模型。 我有一个数组与对象ID。 我想获取所有与我所拥有的ID数组“交叉”的用户logging。 User.find({ records with IDS IN [3225, 623423, 6645345] }, function….

mongoose – validation电子邮件语法

我有一个mongoose用户架构(UserSchema),我想validation电子邮件是否有正确的语法。 我目前使用的validation如下: UserSchema.path('email').validate(function (email) { return email.length }, 'The e-mail field cannot be empty.') 但是,这只会检查字段是否为空,而不是语法。 是否已经存在,我可以重新使用,或者我必须拿出我自己的方法,并在validationfunction内调用?

MongoError,错误:E11000重复键错误

我有这样的MongoDb模式 var User = new Schema({ "UserName": { type: String, required: true }, "Email": { type: String, required: true, unique: true }, "UserType": { type: String }, "Password": { type: String } }); 我想创build一个新的用户这是在使用mongoose ODM的NodeJs中完成的这是创build的代码: controller.createUser = function (req, res) { var user = new models.User({ "UserName": req.body.UserName.toLowerCase(), "Email": req.body.Email.toLowerCase(), "UserType": req.body.UserType.toLowerCase() }); models.User.findOne({ 'Email': […]