Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:查找数组中对象的查询_Mongodb - Fatal编程技术网

MongoDB:查找数组中对象的查询

MongoDB:查找数组中对象的查询,mongodb,Mongodb,我的目标是使用node.js服务器对mongodb进行查询,以获取模式中数组中所有遵守约束的对象 这是我的模式 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var config = require('../../config.js'); module.exports = mongoose.model(config.DATA_TYPE.ROOM, new Schema({ name: String,

我的目标是使用node.js服务器对mongodb进行查询,以获取模式中数组中所有遵守约束的对象

这是我的模式

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var config = require('../../config.js');

module.exports = mongoose.model(config.DATA_TYPE.ROOM, new Schema({
    name: String,
    guide: String,
    leader: String,
    partecipants_counter : { type: Number, default: 0},
    event_counter : { type: Number, default: 0},
    creation: { type: Date, default: Date.now},
    partecipants: [],
    events : [ {
            id : Number,
            data: String,
            user: String
            } ]
}));
我想做一个查询,比如:

SELECT events FROM room WHERE events.id > myInput
我开始编写查询,但缺少
events.id>lastEventId
条件:

getLastEventsById: function(roomId,lastEventId,callback){
        Room.findById(roomId,{name: 0, __v: 0, guide: 0, leader: 0, creation: 0, partecipants_counter: 0, partecipants: 0},callback);
    }
为了更好地理解,这就是我的代码(我想删除for循环)现在的工作方式:

getLastEventsFromId:函数(roomId、lastEventId、回调){
this.getAllEvents(roomId、函数(err、数据){
var事件发送=[];
对于(var i=lastEventId;i
要在模型的子文档中查找,请使用model.find('events.id':)正如您在我的for循环中所看到的,我不是只查找一个id,而是查找id>lastEventId的事件子数组。请尝试查询操作符()
model.find('events.id':{$gt:})
getLastEventsFromId: function(roomId,lastEventId,callback){
        this.getAllEvents(roomId,function(err,data){
                var events_to_send = [];
                for(var i = lastEventId ; i < data.event_counter; i++) events_to_send.push(data.events[i]);
                callback(err,events_to_send);
        });
    },
getAllEvents: function(roomId,callback){
        Room.findById(roomId,{name: 0, __v: 0, guide: 0, leader: 0, creation: 0, partecipants_counter: 0, partecipants: 0},callback);
    }