我想知道是否有办法强制一个唯一的收集入口, 但只有入境不为空 。 e示例架构: var UsersSchema = new Schema({ name : {type: String, trim: true, index: true, required: true}, email : {type: String, trim: true, index: true, unique: true} }); 在这种情况下,“电子邮件”不是必需的,但如果保存“电子邮件”,我想确保此项是唯一的(在数据库级别)。 空条目似乎得到的值为'空',所以没有电子邮件崩溃与'唯一'选项(如果有一个不同的用户没有电子邮件)每个条目。 现在我正在解决它的应用程序级别,但很想保存数据库查询。 谢谢
我想通过ID( 56e641d4864e5b780bb992c6和56e65504a323ee0812e511f2 )显示产品,并在折扣减价后显示价格(如果有的话)。 我可以使用聚合来计算最终价格,但是这将返回集合中的所有文档,如何使其仅返回匹配的id "_id" : ObjectId("56e641d4864e5b780bb992c6"), "title" : "Keyboard", "discount" : NumberInt(10), "price" : NumberInt(1000) "_id" : ObjectId("56e65504a323ee0812e511f2"), "title" : "Mouse", "discount" : NumberInt(0), "price" : NumberInt(1000) "_id" : ObjectId("56d90714a48d2eb40cc601a5"), "title" : "Speaker", "discount" : NumberInt(10), "price" : NumberInt(1000) 这是我的查询 productModel.aggregate([ { $project: { title : 1, price: { $cond: { if: {$gt: ["$discount", […]
以下是我在user.js模型中的user架构 – var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, password: { type: String, require:true }, }, facebook: { id : { type: String }, token : { type: String }, email : { type: String }, name : { type: […]
有没有办法删除Mongoose中的父项的所有子项,类似于使用MySQL的外键? 例如,在MySQL中,我将分配一个外键,并将其设置为级联删除。 因此,如果我要删除一个客户端,所有的应用程序和关联的用户也将被删除。 从顶层: 删除客户端 删除抽奖 删除提交 抽奖和提交都有一个client_id字段。 提交中有一个sweepstakes_id和client_id的字段。 现在,我正在使用下面的代码,我觉得有一个更好的方法。 Client.findById(req.params.client_id, function(err, client) { if (err) return next(new restify.InternalError(err)); else if (!client) return next(new restify.ResourceNotFoundError('The resource you requested could not be found.')); // find and remove all associated sweepstakes Sweepstakes.find({client_id: client._id}).remove(); // find and remove all submissions Submission.find({client_id: client._id}).remove(); client.remove(); res.send({id: req.params.client_id}); });
我有一个来自mongoose的文档,发现我想在JSON编码之前扩展并作为响应发送出去。 如果我尝试向文档添加属性,它将被忽略。 这些属性不会出现在Object.getOwnPropertyNames(doc)使得正常的扩展是不可能的。 奇怪的是, JSON.parse(JSON.encode(doc))工作并返回一个对象与所有正确的属性。 有没有更好的方法来做到这一点?
文档: { _id: 5150a1199fac0e6910000002, name: 'some name, items: [{ id: 23, name: 'item name 23' },{ id: 24, name: 'item name 24' }] } 有没有办法从数组中拉出特定的对象? IE浏览器如何从items数组中拉出ID为23的整个项目对象。 我努力了: db.mycollection.update({'_id': ObjectId("5150a1199fac0e6910000002")}, {$pull: {id: 23}}); 不过,我很确定,我没有正确使用“拉”。 从我所了解的拉将从一个arrays,而不是一个对象拉场。 任何想法如何将整个对象拉出arrays。 作为一个奖金,我试图在mongoose/ nodejs这样做,以及不知道这种types的东西在mongooseAPI,但我找不到它。
我想删除一些MongoDB集合,但这是一个asynchronous任务。 代码将是: var mongoose = require('mongoose'); mongoose.connect('mongo://localhost/xxx'); var conn = mongoose.connection; ['aaa','bbb','ccc'].forEach(function(name){ conn.collection(name).drop(function(err) { console.log('dropped'); }); }); console.log('all dropped'); 控制台显示: all dropped dropped dropped dropped 什么是最简单的方法来确保all dropped将被打印所有集合已被删除后? 任何第三方都可以用来简化代码。
总节点noob在这里。 我一直在尝试设置一个示例节点应用程序,但每次尝试运行时都会popup以下错误消息: 节点应用程序 Failed to load c++ bson extension, using pure JS version events.js:72 throw er; // Unhandled 'error' event ^ Error: failed to connect to [#$%67890 :27017] at null.<anonymous> (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:553:74) at EventEmitter.emit (events.js:106:17) at null.<anonymous> (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15) at EventEmitter.emit (events.js:98:17) at Socket.<anonymous> (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10) at Socket.EventEmitter.emit (events.js:95:17) at net.js:830:16 at process._tickCallback (node.js:415:13)
我已经尝试使用find和findOne并且都不返回文档。 find返回一个空数组而findOne返回null 。 在这两种情况下都是err 。 这是我的连接: function connectToDB(){ mongoose.connect("mongodb://localhost/test"); //i have also tried 127.0.0.1 db = mongoose.connection; db.on("error", console.error.bind(console, "connection error:")); db.once("open", function callback(){ console.log("CONNECTED"); }); }; 这是我架构: var fileSchema = mongoose.Schema({ hash: String, type: String, extension: String, size: String, uploaded: {type:Date, default:(Date.now)}, expires: {type:Date, default:(Date.now()+oneDay)} }); var Model = mongoose.model("Model", fileSchema); 我的查询在这里: Model.find({},function(err, file) […]
当我尝试更改由Mongoose Query返回的数据的任何部分时,它不起作用。 我昨天试图找出大约2个小时,用各种各样的_.clone() ,使用临时存储variables等。最后,当我虽然我疯了,我find了一个解决scheme。 所以我认为未来有人(fyuuuture!)可能有保存问题。 Survey.findById(req.params.id, function(err, data){ var len = data.survey_questions.length; var counter = 0; _.each(data.survey_questions, function(sq){ Question.findById(sq.question, function(err, q){ sq.question = q; //has no effect if(++counter == len) { res.send(data); } }); }); });