Tag: mongodb

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

嘿所以我想让用户编辑他们的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操作,其他操作可能会交错。 由于其他操作可能会交错,因此我只能假定在发生错误的情况下,数据将处于一种转换状态,其中一些文档被更新,而另一些则不被更新。

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 = […]

使用计数器集合在node-mongodb-native中自动增量

有没有什么方法可以像node-mongodb-native文档中描述的那样实现计数器集合 ? 我试图避免通过嵌套超过9000callback(恕我直言,这听起来不整洁,而不是优雅),但写这个本地驱动程序的好人拒绝执行同步/阻塞调用。 原生驱动程序是否提供了一些方法来调用用户定义的函数并在查询过程中使用它们的返回值? 或者可以有一个替代方法提取序列计数….也许完全从一个ObjectID() ? 有任何想法吗? 编辑: 这不是用于_id字段的(这由db.coll.save(x) ) 我在一个集合中有不同types的文档。 如果你知道我的意思,每一个都需要自己的“types序列”或“types序列”。 我已经实现了这个(加上其他的东西)与afew嵌套调用,如下所示。 doc是来自客户端的JSON.parse 。 我必须用_id (按db.coll.savesorting)和typeserial (当前按db.coll.countsorting,如下所示)将此doc返回给客户端。 ///more nesting Async calls above. db.collection('dox').count( { "type" : doc.type } , function( err , count ) { ///{some err checking code here} doc.typeseq = (1+count); db.collection('dox').save( doc , function( err , doc ) { ///{more code […]

如何testing自己的MongoDB包装

我已经编写了自己的用于Node.js的瘦蒙哥包装,以消除代码重复。 但是,我在使用Mocha和Should运行asynchronousunit testing时遇到问题。 会发生什么情况是,由MongoDB驱动程序而不是Mocha捕获的应用程序库引发的任何exception。 也就是说,Mocha既不会捕获错误,也不会调用done()函数。 因此,摩卡打印出一个错误Error: timeout of 2000ms exceeded 。 包装模块db.js片段 var mongodb = require('mongodb').MongoClient; exports.getCollection = function(name, callback) { mongodb.connect(dbConfig.dbURI, {auto_reconnect: true}, function(err, db) { if (err) return callback(err, null); db.collection(name, {strict: true}, callback); }); }; 摩卡test.js var should = require('should'); var db = require('./db.js'); describe('Collections', function() { it.only('should retrieve user collection', function(done) […]

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访问它。 […]

从ZeroMQcallback调用meteor插入/删除操作引发exception

在指定端口上接收到ZeroMQ消息时,以下Meteor代码段barfs。 但是,如果我更改remove find ,它工作正常。 insert操作也失败。 请注意,如果在callback之外运行, insert和remove成功,例如在pull.on部分的下方。 var Components = new Meteor.Collection("components"); function handle_message(msg) { console.log("pull on message" + msg); Components.remove(); } if (Meteor.isServer) { Meteor.startup(function () { var zmq = Meteor.require("zmq"); var pull = zmq.socket("pull"); pull.bind("tcp://127.0.0.1:7000", function(data) { console.log("Connection received from ZMQ"); }); pull.on('message', handle_message); }); } 例外是: W20130827-21:36:21.800(0)? (STDERR) packages/mongo-livedata.js:1640 W20130827-21:36:21.802(0)? (STDERR) throw […]

用NodeJS检索远程文件进行存储?

我有NodeJS restify安装程序,我正在使用mongoose附件将图像附加到我的用户模型,并将图像存储在S3存储桶中。 我还允许用户使用Passport.JS使用Facebook,Google等进行注册。 问题是mongoose-attachments在调用.attach()函数时需要本地文件引用,PassportJS提供了一个远程URL – 所以我需要下载图像,然后从tmp附加它。 我应该如何用NodeJS来解决这个问题? 有没有一个好的模块,我可以用这个?

使用expressionangularJs和mongoDB但ngResourceangular度不加载

这里是我的MongoDB后端代码 var mongo = require("mongodb"); var DbHost = "127.0.0.1"; var DbPort = mongo.Connection.DEFAULT_PORT; module.exports.list = list; var db = new mongo.Db("ferrari-db",new mongo.Server(DbHost,DbPort,{}),{safe:true}); var collectionName = "carsList"; function list(req,res,callback){ db.open(function(error){ db.collection(collectionName,function(error,collection){ console.log("have the collection"); collection.find().toArray(function(error,data){ if (error) callback(false); else{ callback(true); console.log(data); res.json(data); } }); }); }); } 这是我的Node.jsexpression代码 var express = require('express'); var http = require('http'); […]