Tag: mongodb

为什么不在db.collection()中callback?find()。toArray(callback)在数据库连接被删除的情况下调用?

我有这个代码: db.collection('users').find({ active: true ).toArray(callback); 其中db是一个打开的数据库连接到我的本地mongodb服务器。 这工作正常。 但是,如果我手动closures我的MongoDB服务器,而我的应用程序仍在运行,然后执行这一行代码,callback永远不会执行,即使有错误。 这是预期的行为? 我能做些什么来使我的应用程序对丢弃的数据库连接作出反应? 编辑: 我试过这个: db.collection('users') .find({active: true}, function(err, cursor) { if (err) callback(err); else cursor.toArray(callback); }); 再说一次,当连接处于活动状态时,这将起作用,但是当连接被删除时,find()的匿名callback函数会以'err'为空来调用,并且根本不会调用.toArray的callback函数。 如何重写我的代码,以便我得到一个错误,如果db连接不活着?

Mongodb通过自定义ID查找会话集合的结果

我正在使用Mongojs库,它是mongodb库周围的薄包装。 我想在会话集合中查找一些查询,这意味着我使用mongo来存储会话(connect-mongo)。 问题是这样的 db.sessions.find({}, cb); — returns all the sessions 当我尝试通过从req.sessionID – string中获取的id来查找指定的会话时,这将始终返回null 。 db.sessions.findOne({ _id: req.sessionID }, cb); — yes the seesionId exists, when i execute this query manually it works 还尝试了“mongodb”库来直接连接。 var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, […]

mongoose保存embedded的文件

我有以下架构: var StorySchema = new Schema({ title: { type: String, required: true }, users: { id: { type: Schema.ObjectId, ref: 'Users' }, creator: { type: Boolean } }, maxlines: { type: Number, default: '10'}, lines: { text: { type: String }, entered_at: { type: Date }, user: { id: { type: Schema.ObjectId, ref: 'Users' } […]

MongoDB的安全性,我错过了什么?

如果你想用Node JS实现一些东西,Mongo DB&Express是否足够安全地连接到Mongo DB? 有人可以从安全angular度解释这个代码吗? === 许多教程只是使用… var mongoClient = new MongoClient(new Server('localhost', 27017)); Mongos文件包括… var MongoClient = require('mongodb').MongoClient; // Connect to the db MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) { if(!err) { console.log("We are connected"); } }); === 代码基于Mongo文档 – http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html#getting-that-c​​onnection-to-the-database

如何在mongo中为.js脚本编写一个for循环来传递文本文件中的数据库名称variables

我有如下所示的.txt文件中的数据库名称列表 本地 testing pipe理 我怎样才能dynamic地传递参数,而不是在单声道的.js脚本中硬编码。 db = db.getSiblingDB('test'); date = new Date() date.setDate(date.getDate() – 1) yesterday = Math.floor(date.getTime()/1000).toString(16) db.coll.find({_id : {$gt : new ObjectId(yesterday + "0000000000000000")}} , {_id:1})

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} }); 问题 目前我有: […]

NodeJS + MongoDB ASYNC结果出现较晚

我有一个简单的函数,它返回一个通道中的连接用户列表,我在初始阶段用一个简单的数组来使用它,但是知道,我想从mongodb数据库中检索一些用户细节,并将其添加到该数组中。 我遇到的问题是返回给出了一个空的结果,我可以在控制台日志中清楚地看到,这是因为查询运行较晚,并且在返回结果之前运行返回。 我知道这是因为asynchronousfunction,我已经尝试与Q和其他人,但我只是简直不能得到它的工作。 🙁 //GET USERS FOR SPECIFIED ROOM function get_room_users( room ){ // create an array to hold all the usernames of the poeple in a specific room var roomusers = new Array(); // get all the clients in 'room1′ var clients = io.sockets.clients( room ); var i = 0; for(var i = 0; […]

在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: { […]

快速路线在一定数量的Angularjs Post后停止工作

我在获取AngularJS,Express和MongoDB时遇到了一些麻烦。 我对这三者都还是陌生的,并试图做一个简单的“做”应用程序,并切换一个检查button来标记完成。 我能够在angular度上执行一个函数,它将执行一个$ http'POST'到最终更新MongoDBlogging的高速路由。 我可以在停止工作之前做6次。 我可以在巡视控制台看到函数仍然被调用,但在$ http“POST”和执行路由之间的某个地方,它不再注册。 我不再从路由中看到Express服务器上的console.log活动。 这是我的toDoController.js toDoApp.controller('ToDoController', function ToDoController($scope, $http, itemsData) { … other functions $scope.toggleToDo = function(item) { console.log('updateToDo()'); console.log(item); $http({ method: 'POST', url: '/todolist/toggle_to_do', data: item }). success(function(data, status, headers, config) { console.log('success post'); }). error(function(data, status, headers, config) { console.log(data); console.log(status); console.log(headers); console.log(config); }); } } 快递app.js路线 app.post('/todolist/toggle_to_do', todolist.toggleToDo); […]

Node.js中mongodb模块上的不同语法

我现在正在学习如何在Node.js中使用MongoDB,但是据我所知,有两种编写代码的方法。 一个(在一些书和在线博客): var Db = require('mongodb').Db, Connection = require('mongodb').Connection, Server = require('mongodb').Server; 两个(Github页面及其在10gen的文档页面): var MongoClient = require('mongodb').MongoClient; 为什么会出现差异,如果除语法之外还有其他差异,我应该采取哪一种? 也许是由于模块的不同版本,但如果是这样的话,我必须采取哪一个呢? 谢谢。