Tag: mongodb

用Node.js同步连接到MongoDB

通常你会做这样的事情: var db = new mongo .Db('test', new mongo.Server('127.0.0.1', 27017), {w: 1}) .open(function (error, database) { if (error) throw error console.log('Connected to database test') }) 然而,我想同步连接到mongodb。 所以我需要一个我可以用这种方法的方法: var db = new mongo.openDb('test', new mongo.Server('127.0.0.1', 27017), {w: 1}) console.log('Connected to database test') 有没有这样的事情?

将由mongoose返回的数据转换为json

我正在用routes / index.js打印我的整个集合 exports.create = function(req,res){ new checklist_model({ name : req.body.name, comment : req.body.comment }).save(function(err,checklist_model){ res.redirect('/'); }); }; exports.checklist = function ( req, res ){ checklist_model.find({},function ( err,checklist_data,count ){ res.render( 'checklist', { title : 'checklist', checklist_data : JSON.stringify(checklist_data) });console.log(checklist_data); }); }; 我以下面的格式获取收集数据: { name: 'Stats', comment: 'Functionality test', _id: 53c5f7cdb79a40232193c45e, __v: 0 } 我如何得到这个以JSON格式打印? { […]

测验网站的Mongo模式

我正在构build一个小型的Node / Mongo应用程序,每天为用户提供最多3个问题。 用户只能回答是或否,正确的答案将在以后确定(这些问题更接近预测)。 目前,我有这些文件: 用户 ID 题 ID 问题答案 ID question_id(ref) UserAnswer ID question_id(ref) user_id(ref) 什么是最有效的方式来查询分贝,所以我得到今天的问题,但也检查是否该用户已经回答了这个问题已经? 我觉得我正在超越它。 我已经尝试了一些似乎是矫枉过正的方法。

将XXX.db.bson_serializer.ObjectID.createFromHexString(id)外包给函数?

我经历了这个快速教程。 我想知道是否有可能将以下呼叫外包给一个单独的function,因为它非常长? employee_collection.db.bson_serializer.ObjectID.createFromHexString(id) 这是调用语句的整个文件: var Db = require('mongodb').Db; var Connection = require('mongodb').Connection; var Server = require('mongodb').Server; var BSON = require('mongodb').BSON; var ObjectID = require('mongodb').ObjectID; EmployeeProvider = function(host, port) { this.db = new Db( 'node-mongo-employee', new Server(host, port, {}), {safe: true} ); this.db.open(function(){}); }; … // find an employee by id EmployeeProvider.prototype.findById = function(id, callback) { […]

mongoose自定义validation失败

在我的应用程序中,我试图在mongoose上运行一些自定义的validation,我想要的只是能够确保来自特定用户的评级不应超过一次,我已经尝试了几件事情,并且开始了代码正确返回true和false,但错误不会被触发。 这是我的代码 RatingSchema.path('email').validate(function (email) { var Rating = mongoose.model('Rating'); //console.log('i am being validated') //console.log('stuff: ' + this.email+ this.item) Rating.count({email: this.email, item: this.item},function(err,count){ if(err){ console.log(err); } else{ if(count===0){ //console.log(count) return true; } else { //console.log('Count: in else(failing)'+ count) return false; } } }); },'Item has been already rated by you')

如何检查模式已经在mongo数据库中创build?

我是mongo Db的新手,需要查看是否已经创build了模式。 我正在使用mongoose和node.js。 如果没有创build,我需要以某种方式运行创build脚本一次,否则继续与其他的东西。 TIA

为MongoDB查询duong,但允许某些基于时间戳的重复项

所以我有一组数据与时间戳相关联。 我想让mongo在3分钟的时间戳内重复那些重复的。 我会告诉你一个我的意思的例子: 原始数据: [{"fruit" : "apple", "timestamp": "2014-07-17T06:45:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:47:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:55:18Z"}] 查询后,会是: [{"fruit" : "apple", "timestamp": "2014-07-17T06:45:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:55:18Z"}] 因为第二个条目是在第一个条目创build的3分钟内。 我已经得到了代码,以便聚合和删除具有相同水果的模糊,但现在我只想结合在时间戳泡沫内的模糊。

MongoDB元素的顺序

我的应用程序有一个游戏列表。 每个游戏都有一个位置(向用户显示的顺序是:1,2,3 …)。 我正在使用MongoDB来保存所有的数据。 好的,所以现在我有了位置4的游戏。 我想把它改成位置1.那么我需要做什么? 我需要在MongoDB中将这个游戏的场地位置从4更新为1,并且对位于1之后的这个集合中的所有文档进行增量。 但是我觉得这不是个好主意! 可能你有想法如何更好地实施它? 谢谢! 蒙戈表的例子: _id | name | description | position | ________________________________________________________ ObjectId("…") | bubble | some description | 1 ObjectId("…") | bubbleA | some description | 2 ObjectId("…") | bubbleB | some description | 3 ObjectId("…") | bubbleC | some description | 4 ObjectId("…") | bubbleD […]

节点通用池错误

我只是试图在我的博客应用程序中使用节点池 ,这里是我的代码: db.js module.exports = function () { return new Db(settings.db, new Server(settings.host, Connection.DEFAULT_PORT), {safe: true}); } post.js var Db = require('./Db'); var pool = poolModule.Pool({ name : 'mongoPool', create : function(callback) { var mongodb = Db(); callback(null, mongodb); }, destroy : function(mongodb) { mongodb.close(); }, max : 100, min : 3, idleTimeoutMillis : 30000, […]

如何在sails中保证索引

我已经使用sails.js框架编写了一个应用程序。 http://sailsjs.org/#!documentation我无法pipe理在控制器中运行ensureindex。 在sails.js中运行ensureIndex查询的方法是什么?