Tag: mongoose

mongoose子文档同时满足多个条件

我有一个架构与Resource , Skill ,和ResourceSkillLevel 。 前两个基本上只是名字,而后者则是对资源,技能和水平的参考。 我想同时select所有符合特定技能水平的Resource 。 架构 // Omitting fields that don't matter for the purpose of the question ResourceSchema = new Schema({name: String}); SkillSchema = new Schema({name: String}); ResourceSkillLevelSchema = new Schema({ "resource": {type: Schema.ObjectId, ref: "Resource"}, "skill": {type: Schema.ObjectId, ref: "Skill"}, "level": {type: Number, min: 1, max: 5} }); 问题 目前我有: […]

在Mongoose中填充inheritance的文档

我正在尝试为以下模型创build一个数据库模式: 我不知道什么是更好的方式来表示这个在MongoDb将是,但因为我使用Mongoose和有一个插件的inheritance,我想下面的: var mongoose = require('mongoose') , extend = require('mongoose-schema-extend') , Schema = mongoose.Schema , ObjectId = mongoose.Schema.Types.ObjectId var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { //Mashup and Item are bound (itemSchema is a sub-doc) var itemSchema = new Schema({ pos: { top: Number, left: Number } , size: { […]

如何让用户更新他们的个人资料?

嘿所以我想让用户编辑他们的configuration文件。 但是,有些字段并不总是存在于文档中,除非添加该字段。 这是我目前如何调用查询,但如果该字段不存在,我会得到一个错误。 这里是路由文件的代码: User.findByIdAndUpdate(req.signedCookies.userid,{ firstName: req.body.firstName.toLowerCase(), lastName: req.body.lastName.toLowerCase(), email: req.body.email.toLowerCase(), firstNameTrue: req.body.firstName, lastNameTrue: req.body.lastName, emailTrue: req.body.email, emailList: req.body.newEmail, phone: req.body.phone, phoneList: req.body.newphone, socialAccounts: {socialAccount: req.body.socialAccount, socialAddress: req.body.socialAccountNew}, currentCity: req.body.currentCity, birthday: new Date(req.body.birthday) }, function(err, user) { console.log('here1'); if(err) { console.log("post2"); console.log(err); res.render('editUserProfileError', {title: 'Weblio'}); } else { console.log("post3"); res.redirect('userProfile'); } }); }; 我得到的错误是: [TypeError: […]

mongoose:如果发生错误,我可以假设我的数据

假设我正在通过Mongoose进行单一收集,多文档操作,例如 // all my_model doc's have flag==false here my_model.update({flag:false}, {flag:true}, {multi:true}, function(err) { if (err) { // what can I assume here? } }); 如果发生错误,我可以说我的my_model文件吗? 会有一些flag == true ? 或者这是一个primefaces操作,如果出现错误,则不会发生任何更新? 这种行为是否与其他具有单一集合的多文档操作一致(ex remove() )? 这是我为非酸性支付的价格吗? 编辑 :从mongodb文档: 即使写入操作修改了该文档中的多个子文档,对单个文档的修改也始终是primefaces的。 对于修改多个文档的写操作,整个操作不是primefaces操作,其他操作可能会交错。 由于其他操作可能会交错,因此我只能假定在发生错误的情况下,数据将处于一种转换状态,其中一些文档被更新,而另一些则不被更新。

在Expresspath文件中创build用户时出错

我正在创build一个节点应用程序,我正在遵循nodepad示例来创build我的用户。 但是,当我尝试打开我的/注册页面时,我正在尝试执行路由分离并遇到问题。 我的app.js文件看起来像这样: var express = require('express'), app = express(), users = require('./routes/users'), http = require('http'), path = require('path'), mongoose = require('mongoose'), mongoStore = require('connect-mongodb'), util = require('util'), models = require('./models'), db, User, LoginToken, Settings = { development: {}, test: {}, production: {} }; /** * Environments */ app.configure('development', function() { db = 'mongodb://localhost/db-development'; app.use(express.errorHandler({ […]

MongoDB:如何按价值查找关键字

我正在使用expression框架和Mongoose(MongoDB)Node.js,我有一个关于如何有效地检索数据的问题。 假设我在mongo文件上有这样的东西: test : {a:1, b:2, c:2, d:1}; 检索键(a,b,c或d)的值很容易,但是如何做倒数,例如检索所有的值为2的字母(在我的例子中是'b'和'c' ) 谢谢!

无法将值返回到mongoose / mongodb和nodejs的响应中

我通过Mongoose使用Nodejs,ExpressJs,MongoDB。 我创build了一个简单的UserSchema。 我有我的代码分成多个文件,因为我预见他们变得复杂。 url'/ api / users'被configuration为调用'routes / user.js'中的列表function,这些按预期发生。 UserSchema的列表函数确实被调用,但它没有返回任何东西给调用函数,因此没有结果。 我究竟做错了什么 ? 我试图基于http://pixelhandler.com/blog/2012/02/09/develop-a-restful-api-using-node-js-with-express-and-mongoose/ 我想我做了userSchema.statics.list的函数定义的错误 app.js users_module = require('./custom_modules/users.js'); // I have separated the actual DB code into another file mongoose.connect('mongodb:// ******************'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback() { users_module.init_users(); }); app.get('/api/users', user.list); custom_modules / users.js function init_users() { userSchema = […]

Mongoose.js无法从我的文档中获取数组

在我的mongoose模型中,我有这样的用户架构: var userSchema = mongoose.Schema({ _id : String, username: String, name : String, timestamp : { type : Date, default: Date.now }, admin : Boolean, pages : [String] }); var User = mongoose.model('User', userSchema); 我试图从这个文件中得到这样的页面数组: function isUserPage(userId, pageId, callback) { models.User.find({_id: userId}, function(err, user) { console.log('user pages: ' + JSON.stringify(user[0].pages)); … }); 问题是我的console.log正在输出[[object object]]。 我可以看到数据在烟雾(一个基于web的mongodbpipe理查看器)中的数据,但我似乎无法用JavaScript访问它。 […]

asynchronous每个系列不会按预期返回

即时通讯使用asynchronous库,特别是每个方法,但我没有得到预期的结果。 function instanceMethod (final_callback) { obj = this; async.parallel([ function (callback) { getTopReplies (obj, function (err, result){ if (err) return callback(err); if (result) { obj.topReplies = result; callback(); } }); } // … theres actually more 2 functions here but // I removed them for the sake of space and // readability, ], function (err){ […]

为什么茉莉节点mongoose检测不等,像预期的那样?

我正在写一个简单的应用程序,保存和查找位置。 我正在使用mongoose和茉莉节点。 用户CRUDtesting按预期工作。 但是,我个人创build了用户来testing不同的自定义validation。 我也通过清除收集和重新加载所有用户来开始testing,以确保在启动save / update / etctesting之前,所有testing数据都是好的。 对于位置,我做的是一样的,但我有几十个位置,我想用数组加载它们…并testing负载沿途,以确保它工作正常。 如果我只做一个位置,它工作正常。 不止一个,他们失败了。 我知道我错过了一些asynchronous相关的步骤在这里,但我要么search错误的条款,要么我现在太接近它看到我在这里做的根本简单的错误。 版本: mongoose:3.6.16 茉莉花节点:1.11.0 mongodb:2.4.5 细节testing… it("creating location succeeds", function(done){ for(var locIndex in testLocations) { locations.create(testLocations[locIndex], function(err, location){ // console.log says location is undefined // unless there is only one location, then it works. expect(err ).toBeNull(); expect(location.name ).toBe(testLocations[locIndex].name); done(); }); } }); …和创buildfunction从一个单独的文件举行位置相关的function… […]