Tag: mongodb

Node.js find()在Mongoose模型定义中

我想在我的模型上定义一个方法,包括search同一个模型的文档,这里是我的尝试: var mongoose = require('mongoose'); var Author = require('./author.js'); var bookSchema = mongoose.Schema({ author : { type: mongoose.Schema.Types.ObjectId, ref: 'author' }, genre: String, }); bookSchema.methods.findSimilar = function(callback) { bookSchema.find({'genre': this.genre}).exec(function doThings(err, doc){ /* … */ }); }; module.exports = mongoose.model('book', bookSchema, 'book'); 但是,我得到TypeError: bookSchema.find is not a function 。 我也尝试了bookSchema.methods.find() ,结果相同。 我怎样才能解决这个问题? 谢谢, 编辑:启发了这个答案 ,我也尝试this.model('Book').find() […]

在MongoDB文档中使用MomentJS作为默认date

我正在尝试使用MomentJS获取非本地化的UTC时间,并将其设置为在Mongo中创build的新文档的默认值。 var SubFavoriteSchema = new Schema({ user : { type: String, ref: 'Account'}, date : {type: Date, default: moment.utc()} }); 问题是,moment.utc()返回节点服务器启动的date。 因此,如果我在1月1日启动服务器,即使文档是在1月10日创build的,所有文档都会获得1月1日的UTC时间。 任何想法为什么它不断获取服务器的开始时间而不是当前时间?

如何在mongo db中拼合子文档

我的collections有这样的文件 { "_id" : ObjectId("587c8d0364b6e32706f7edef"), "first_name" : "John", "last_name" : "Doe", "password" : "aasdjsabb12213b21bbcghc1h2", "shift" : "A", "dept" : "Management" "Requests" : [ { "weekId" : 1, "MO" : 1, "TU" : 2, "W" : 3, "TH" : 9, "FR" : 10, "SA" : 6, "SU" : 1 } ] } 我想将我的查询结果导出到csv,并需要像这样平铺的字段 { "_id" : […]

如何从mapReduce mongoose中的关键字获得近似值?

我在MongoDB中有一些文件,例如: { "_id" : ObjectId("587f41ffb5ffa43ec25aa075"), "IP" : "113.183.149.127", "Product_Version" : "CMC_2.0", "Product_ID" : "HHFFHJRLAUHOZDBM1YWY291NM0DD7J40", "OS" : "Microsoft_Window_7", "virus" : "Adware:W32/Superfish", "Time" : ISODate("2017-01-18T10:22:55.356Z"), "city_name" : "", "location" : { "latitude" : 0.0, "longitude" : 0.0 } } { "_id" : ObjectId("587f41ffb5ffa43ec25aa078"), "IP" : "27.76.109.254", "Product_Version" : "CMC_2.0", "Product_ID" : "UCOV8JY5YHKQ2B0HPCC0IM1S9T0ICWL7", "OS" : "Microsoft_Window_7", "virus" : […]

如何使用mongoose运行原始mongoDB命令?

我可以使用下面的命令改变mongodb的sort-buffer-size db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: <limit in bytes>}) 但是,如何使用mongoose库运行相同的命令?

无法获取我的数据库(MongoDB)的数据输出在我的网站上

我试图从我的mongoDB数据库中获取数据。 并输出他们在一个简单的网站,不能弄清楚如何。 当我使用直接input到contactlist数组中的“虚拟数据”时,我得到了它的工作。 而只是取而代之。 但现在我有简单的数据库,其中包括: [ { _id: 5880e3a737893c04c87e30bf, name: 'Tom', email: 'Tom@msn.com', number: '4444444444' }, { _id: 5880e44637893c04c87e30c0, name: 'Tracy', email: 'Tracy@asodj.com', number: '123412345' }, { _id: 5880e44637893c04c87e30c1, name: 'tucker', email: 'tucker@email.com', number: '1234456666' } ] server.js : var express = require('express'); var app = express(); var mongojs = require('mongojs'); var db = mongojs('contactlist', […]

数组types的mongodb字段在使用.push()时不会更新长度

以下是试图将用户添加到模式的代码: var roomSchema = new Schema({ name: { type: String, required: true}, connections: { type: [ { userId: String } ] }, content: { type: String, default: ""}, isFull: { type: Boolean, default: false} }); roomSchema.methods.addUser = function(username, callback) { this.updateAsync( { $push: { connections: { userId: username } } } ) .then(function(status) { return […]

使用router.js中的promise来模拟error handling

我正在尝试将传感器值保存到我的mongodb数据库。 我正在使用承诺做适当的error handling。 我正在使用then()和catch()但我想我没有做正确的。 我发送一个组ID,以便检查组ID是否存在,然后只存储传感器信息。 这是我的代码。 router.post('/data/:group_id', function(req,res,next){ var group_id = req.params.group_id; User.find({group_id : group_id}).then(function(user){ var object_id = user[0]._id; var datas = req.body; var data = new Data({ user : object_id, value: datas.value, valueString : datas.valueString, sensorStatus : datas.sensorStatus, timeStamp : new Date().toJSON() }); data.save().then(function(data){ res.send('Data Saved'); }); }).catch(function(e){ if(e.code === undefined){ res.send('Group id does […]

使用switch case更新集合中的多个文档

我在我的NodeJS应用程序中使用MongoDB本地驱动程序。 我在我的数据库中有一个需要更新的shifts集合。 在我的class次集合中的示例文档 { "_id" : ObjectId("588425105560bd2ba0065fa4"), "from" : ISODate("2017-01-23T03:20:00.000Z"), "to" : ISODate("2017-01-23T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } { "_id" : ObjectId("588425105560bd2ba0065fa5"), "from" : ISODate("2017-01-25T03:20:00.000Z"), "to" : ISODate("2017-01-25T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } 我需要做的是以下 – 更新符合条件的所有文档的hourlyRate : 匹配jobId(很简单) 设置hourlyRate = 20如果from是Weekday 设置hourlyRate = 25如果from星期六开始 设置hourlyRate = 30如果from星期天开始 我想尽可能在​​单个查询中完成。 我的解决scheme迄今为止 使用开关大小写,并使用date聚合函数中的$dayOfWeek确定datetypes。 但是,我无法将交换机与updateMany结合使用。 […]

types对象的sailsjs模型属性,可能吗?

以下是我的帆模型,每当我尝试保存()我得到以下错误 [Error: Unknown rule: address] 使用与mongoose类似的模型,没有任何问题,有人可以看到我的模型中的任何错误? 谢谢! /** * Users.js * * @description :: TODO: You might write a short summary of how this model works and what it represents here. * @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models */ module.exports = { connection: 'MongoServer', attributes: { AcctSessionId: {type: 'string', required: false, default: ''}, UserName: {type: 'string', required: true, […]