Tag: mongodb

MongoDB与Mongoose – select一个指定的date范围丢弃时间

我有一个mongoose模式组成的数组与date字段的元素 var querySchema = new Schema({ id : String, description: String, results : [{ date : { type: Date, default: new Date }, result: Object }] }); 当我添加新的元素,我使用New Date 。 导致如下的元素: { "_id" : ObjectId("55b96289c9dcbe79207c4b08"), "id" : "11", "description" : "List of graphs loaded in the KB (24 graphs as of 09/05/2015 + 5 default […]

从表单数据中设置Mongoose模型属性

我对mongodb / mongoose很新。 所以新的,我什至不知道什么话给谷歌find我的问题的正确答案。 我有一个表格,它接受关于歌曲的信息。 'use strict'; /** * Module dependencies. */ var mongoose = require('mongoose'), Schema = mongoose.Schema, soundcloud = require('soundcloud-resolve'); /** * Song Schema */ var SongSchema = new Schema({ name: { type: String, default: '', required: 'Please fill Song name', trim: true }, created: { type: Date, default: Date.now }, user: { […]

Mongodbasynchronous获取结果

这是我的行动,将从用户收集所有用户 app.post('/login', function(req,res,next){ users = self._db.get('users', {}) }) 这是我在数据库类中的function this.get = function( col, opt ) { var result = [], opt = opt || {}; query = db.collection(col).find( opt ); query.each( function(err, doc) { console.log( doc ); if( doc != null ) { result.push( doc ); } }); return result; }; 当我login用户对象它返回空数组,但是当我logging每个文件内function它成功的工作 问题是我怎样才能得到asynchronous的结果?

将数据添加到Mongodb集合给出错误

我正在使用云9 ide来开发nodejs应用程序。 在这个我使用的是mongodb。 我已经创build了数据库,并且连接成功。 现在我要添加logging到collections app.post('/ajax-mongo-connect', function (req, res) { MongoClient.connect("mongodb://localhost:27017/sedd", function (err, db){ if(!err) { console.log("Mongo Connected."); db.test.insert({ item: "card", qty: 15 }); //console.log(drp); res.send('connected'); } else { console.log("Mongo not Connected"); res.send('error'); } }); }) 试图插入{ item: "card", qty: 15 }来testing集合。 错误发生如下 /home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:475 throw err ^ TypeError: Cannot call method 'insert' of undefined at […]

如何在mongoose模型中使用find方法的callback结果

我使用mongoose作为nodejs-mongodb应用程序的ODM。 是我的第一个nodejs应用程序,我来自一个非函数式的编程背景。 看着mongoose的文档 ,你可以find: Kitten.find(function (err, kittens) { if (err) return console.error(err); console.log(kittens); }); 好极了,在这里我们有find函数作为我们模型的一部分(Kitten),它实际上可以find文档,并在callback函数中将其作为“kittens”检索,在这种情况下,它使用console.log()。 但是我想知道如何使用这个函数式编程来将这个值赋给一个variables(因为我在模型文件夹的另一个文件中有这个值,并且导入了需要的模块) 我发现了另外一个关于ObjectId类似请求的问题,但是当您使用callback来使用console.log来打印答案时,它们提供了同样的答案。如果我们诚实地说,这是没有用的。 因为我来自一个非函数式的编程背景,所以我期望的是: var kitten = Kitten.find({name:'Silence'}); 据我所知,如果你在callback中指定了一个新的variables,variables的作用域就在callback函数中,和返回variables一样,甚至在方法不能工作之前声明一个variables。 我确定有什么我错过了。 这个项目是如此之大,我不认为他们不能忘记提供一个方法来做到这一点。 我认为在函数式编程中我有一些东西缺less,或者我不知道。 那么,怎样才能做到这一点呢? 谢谢 ! 编辑:我不知道为什么社区说我的问题是一个可能的重复如何从asynchronous调用返回响应? 这个问题更多地面向js ajaxasynchronous一般,这个问题是面向ODM框架,以及如何处理的结果,它可以通过承诺也是一个不同的东西

mongoose深入查找查询

所以,我有一个名为Post的MongoDB中的集合,就像这样: var PostSchema = new mongoose.Schema({ creator: { type: ObjectId, ref: 'User', required: true }, content: { type: String, required: true }, title: { type: String, required: true }, post_type: { type: String, required: true }, view_type: { type: String, required: true }, comments: [{ type: ObjectId, ref: 'Comment' }], reports: [{ type: ObjectId, ref: […]

nodeJS模块mongoose给我错误

当我运行我的代码我得到一个错误我想要做的是当有人login到我的网站它将IP和其他数据logging到数据库中。 它似乎工作,但后来我得到这个错误,它退出了我的应用程序 { [Error: Trying to open unclosed connection.] state: 1 } Connection to database has been established /home/azura/node_modules/mongoose/lib/index.js:343 throw new mongoose.Error.OverwriteModelError(name); ^ OverwriteModelError: Cannot overwrite `dataType` model once compiled. at Mongoose.model (/home/azura/node_modules/mongoose/lib/index.js:343:13) at Namespace.<anonymous> (/home/azura/Desktop/dbWrite.js:19:37) at Namespace.EventEmitter.emit (events.js:95:17) at Namespace.emit (/home/azura/node_modules/socket.io/lib/namespace.js:205:10) at /home/azura/node_modules/socket.io/lib/namespace.js:172:14 at process._tickCallback (node.js:415:13) 我使用的代码是: var mongoose = require("mongoose"); var express = […]

NodeJS服务器上的Mongoose查询返回NULL

我正在查询Twitter的位置,它已成功返回。 然后这些位置被存储在一个mongodb中。 当我通过命令行查询数据库的位置:“英国”时,位置数据被返回。 但是,当我查询NodeJS服务器上的数据库,没有任何返回(空)。 这里是代码: 'use strict'; // Set default node environment to development process.env.NODE_ENV = process.env.NODE_ENV || 'development'; var express = require('express'); var mongoose = require('mongoose'); var config = require('./config/environment'); var Location = require('./api/twitter/location/twitter/location.model'); // Connect to database mongoose.connect(config.mongo.uri, config.mongo.options); mongoose.connection.on('error', function(err) { console.error('MongoDB connection error: ' + err); process.exit(-1); }); // Populate DB […]

返回玉的checkbox值。 使用node.js,express和Mongoose

我保存了mongoDB中的checkbox值。 将值传递给页面 if (req.session.user.Single === undefined) {single_status= 'false'} else {single_status= ''}; res.render('user-profile', { Single: single_status, )}; 那我在玉里有这个 .col-md-4 label-option(for="checkbox") input(type="checkbox", checked="#{Single}", name='single', id='single') 在呈现的视图。 它出现checked =“false”,所以它总是被检查。 如何将阀门返回到checkbox以显示现有的已选中/未选中? <label-option for="checkbox"> <input type="checkbox" checked="false" name="single" id="single"> Single Status </label-option>

MongoLab上的Node.js和MongoDB:每个插入上的“套接字closures”

我试图做一些相对简单的事情,每当我尝试做一个“插入”,突然间就会遇到“服务器…- a.mongolab.com:36648套接字closures”错误。 读取似乎没有错误的工作,但插入似乎每次都会得到一个错误,我不知道它是我的代码(最近进行了一些小的改变),或者我在MongoLab使用的免费服务器的可靠性问题最近performance出下降几分钟)。 奇怪的是,logging本身似乎保存没问题,我只是把错误回来了! 任何人都可以看到我的代码问题,或者这可能是别的? var mongoClient = require('mongodb').MongoClient; var http = require('http'); var connectionString = "…"; var pictureWallsCollectionName = 'PictureWalls'; //this is what barfs. see *** details exports.saveWall = function (req, res) { //reformat var toSave = { _id: req.body.wallId, pictures: req.body.pictures }; var status; mongoClient.connect(connectionString, function (err, db) { if (err) { return […]