我的目标是使用我的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: 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);
}