Tag: mongoose

Mongoose嵌套的对象数组中的唯一值

对于我的项目,我想为组织团体保留一份mongoose文件,就像这样: var groupSchema = Schema({ name : { type : String }, org : { type : Schema.Types.ObjectId, ref : 'Organization' }, … users : [{ uid : { type : Schema.Types.ObjectId, ref : 'User' }, … }] }); 我想阻止同一个用户在同一个组中两次。 要做到这一点,我需要强制users.uid在用户数组中是唯一的。 我试着说“唯一的:真正的”,但没有奏效。 有没有办法做到这一点与mongoose或mongoDB没有额外的查询或拆分架构? 编辑:我改变了以前的uid值为uid:{type:Schema.Types.ObjectId,ref:'User',index:{unique:true,dropDups:true}}但是,这似乎并没有工作。 编辑:假设没有简单的方法来实现这一点,我添加了额外的查询检查用户是否已经在组中。 这在我看来是最简单的方法。

在Mongoosestring键中存储Json对象

在我的Mongoose模式中,我有一个string是一个string,我想能够在其中存储一个JSON对象。 可能吗? 在Postgres中,可以将字典存储在string列中。 我想这样做是因为字典(实际上是JS中的一个JSON对象)只是一个简单的读写值,不需要查询,而且因为它只是一个值而不是一个值的数组。

date在mongoose?

我知道我可以将Date.now设置为mongoose模式中的默认值,但是其他Javascriptdatetypes或所有date都会自动转换为mongodb的标准格式 – ISOdate ? 在存储date方面有什么我应该关注的吗? dates: { created: {type: Date, default: Date.now}, } 谢谢

如何在mongoose中创build一个新的数据库?

如果我在node.js中使用Mongoose,则需要执行以下操作: var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/myDB'); 但是,如果我刚刚安装了mongodb,而且我目前没有任何数据库呢? 如何在mongoose.connect mongoose.connect('mongodb://localhost/myDB')之前使用mongoose在node.js中创build一个新的数据库? 或者,如果没有这样的myDB那么它会创build一个新的? 或者会抛出一个错误,说没有这样的数据库?

最好的方法来检查mongoosevalidation错误

我有两个validation函数为我的usermodel User.schema.path('email').validate(function(value, respond) { User.findOne({email: value}, function(err, user) { if(err) throw err; if(user) return respond(false); respond(true); }); }, 'EMAIL_EXISTS'); 和username相同 User.schema.path('username').validate(function(value, respond) { User.findOne({username: value}, function(err, user) { if(err) throw err; if(user) return respond(false); respond(true); }); }, 'USERNAME_TAKEN'); 他们以下面的格式返回错误 { message: 'Validation failed', name: 'ValidationError', errors: { username: { message: 'Validator "USERNAME_TAKEN" failed for path […]

十进制/浮点mongoose为node.js

我在node.js / mongoDB / mongoose上启动我的第一个testing应用程序,这是一个非常简单的应用程序,旨在创buildDB中的logging并检索它们。 我创build一个模型,如: var Car = new Schema({ brand : String, speed : Number, date : { type: Date, default: Date.now } }); 这工作正常,除了我希望能够提供一个浮点值的速度,而不是一个整数。 我尝试了十进制和浮点数,但是没有一个在工作。 我也没有在文档中find。 任何想法 ?

集团(在)mongoose?

我已经在shell中构build了我想要的查询,但是在Mongoose中编写它时遇到了麻烦。 db.commentstreams.group({ key: { page_id: true }, reduce: function(obj,prev) { prev.num_comments += obj.num_comments }, initial: { num_comments: 0 } }) 我对Mongoose语法有点困惑; 也许有人可以在这一点上有所了解。 非常感谢。

在MongoDB中删除时自动删除引用对象

假设我有这样的模式: var Person = new Schema({ name: String }); var Assignment = new Schema({ name: String, person: ObjectID }); 如果我删除了一个人,仍然可能存在遗留的孤立的分配,这些分配引用了一个不存在的人,这会在数据库中造成无关紧要的混乱。 有一个简单的方法可以确保当一个人被删除时,那个人的所有相应的引用也将被删除?

Mongoose pre.save()asynchronous中间件没有按预期工作

以下为: Mongoose唯一validation错误types 我使用这个模式从npm的mongoose 3.0.3 : var schema = new Schema({ _id: Schema.ObjectId, email: {type: String, required: true, unique: true} }); 用这个中间件从unique:true获取一个validationError schema.pre("save", function(next, done) { var self = this; model.findOne({email : this.email}, 'email', function(err, results) { if(err) { done(err); } else if(results) { console.warn('results', results); self.invalidate("email", "email must be unique"); done(new Error("email must be unique")); […]

mongoose创build多个文件

我知道在最新版本的Mongoose中,您可以将多个文档传递给create方法,或者在我的情况下更好地传递一组文档。 var array = [{ type: 'jelly bean' }, { type: 'snickers' }]; Candy.create(array, function (err, jellybean, snickers) { if (err) // … }); 我的问题是,数组的大小是dynamic的,所以在callback中有一个创build对象的数组是有帮助的。 var array = [{ type: 'jelly bean' }, { type: 'snickers' }, ….. {type: 'N candie'}]; Candy.create(array, function (err, candies) { if (err) // … candies.forEach(function(candy) { // do some […]