Tag: mongoose

如何在一个数组mongoose中存储不同的子目录?

我有一个testing模型,可以有一个(无线电)和多个(checkbox)答案的问题。 问题存储在数组中。 如何在不closuresvalidation的情况下存储它们(由不同的子目录表示)? 创build一个内部types的subdoc并根据types使用doc进行操作是个好主意吗? checkbox问题示例: {type: 'checkbox', text: 'question text', answers: [ {text: 'first answer', checked: false}, {text: 'second answer', checked: true} ]} 和广播例子: {type: 'radio', text: 'question text', variants: ['wrong answer', 'right answer', 'wrong answer'], answer: 1}//index of right variant

在MongoDB中更新数组中的多个值

我收到一个jsonObject并想执行Mongo-DB更新: jsonObject: "tablename":"1","inventar":[{"ean":"802.6180.222"},{"ean":"657.7412.878"}]} 现有文件(缩写): "tablename": "1", "accepted": false, "inventar": [ { "ean": "802.6180.222", "accepted": "0" }, { "ean": "657.7412.878", "accepted": "0" } ], 我需要为Array中的每个对象(在invetar-jsonObject中)将接受的值设置为“1”。 代码: app.post('/in_accept', function(request,response){ var jsonString=request.body.json; var jsonObj = JSON.parse(jsonString); var InUser = jsonObj.in_user; var InDate = jsonObj.in_date; var inventar = jsonObj.inventar; //is an Array var tablename = jsonObj.tablename; console.log(inventar); var query […]

钩住特定的Mongoose模型查询

我在invoice.js中有自包含的模型 'use strict'; // load the things we need var mongoose = require('mongoose'); var auth_filter = require('../../auth/acl/lib/queryhook'); var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB'); // PROMISE LIBRARY USED FOR ASYNC FLOW var promise = require("bluebird"); var Schema = mongoose.Schema, ObjectId = Schema.Types.ObjectId; // define the schema for our invoice details model var invoicedetailSchema = new Schema({ […]

mongoose:没有保存的对象数组

我正在尝试添加对象id到数组中(mongoose)(Node.js)。 这是我的代码: app.post('/api/users/:userId/favorites/:objectId', function(req, res, next) { User.findByIdAndUpdate(req.params.userId, {$addToSet: {user_favorites: req.params.objectId}}, {safe: true, upsert: true}, function(err, data){ if (err) return res.status(500).send(err) res.status(200).send({'message':'saved'}); }) }) 这是我的模型: module.exports = mongoose.model('User',{ … user_favorites: [{ type: mongoose.Types.ObjectId, ref: 'Property' }], … }) 没有错误被返回,但是ID不被添加到数组中。 我错过了什么?

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深入查找查询

所以,我有一个名为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 […]

MongoDb从对象属性匹配条件的数组中select对象

我对mongoose和nodejs比较新。 而我试图在nodejs中快速的修改服务器端脚本。 我试图从mongoDb使用mongoose检索数据作为对象的数组。 这是我的模式看起来像 – var heatmapSchema = new Schema({ vuid: String coordinates: [ { x: Number, y: Number, c: Number, timestamp: Number } ] }, {collection: collectionName} ); 正如你所看到的, coordinates是一个对象数组。 我想查询mongoDb,所以我得到这些坐标对象的数组,其中c = 1 ( coordinate c属性等于1),即 – [ { x: 100, y: 230, c: 1, timestamp: 1233312312 }, { x: 120, y: 240, c: […]

Mongoose / mongodb – 每个ID只获取最新的logging

我有mongoose的检测模型: var InspectionSchema = new Schema({ business_id: { type: String, required: true }, score: { type: Number, min: 0, max: 100, required: true }, date: { type: Number, // in format YYYYMMDD required: true }, description: String, type: String }); InspectionSchema.index({business_id: 1, date: 1}, {unique: true}); 对同一个业务可能有多个检查(每个业务由一个唯一的business_id表示)。 但是,每个商家每天都有一次检查的限制,这就是为什么business_id +date有唯一的索引。 我还在Inspection对象上创build了一个静态方法,在给定business_ids列表的情况下,检索所有基础业务的检查。 InspectionSchema.statics.getAllForBusinessIds = function(ids, callback) { […]