Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Node.js 使用mongoose.js填充后查询数组_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 使用mongoose.js填充后查询数组

Node.js 使用mongoose.js填充后查询数组,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我试图在填充了引用的相应对象后查询引用数组。我有一种感觉,我要么没有使用正确的操作系统。。。或者我的方法不是正确的方式去做我需要做的事情 我举个例子来说明我到底想做什么。假设我有一组用户,他们可以有多个与他们相关联的预订文档。我需要找到所有用户谁住在某个位置,并有至少一个预订的开始日期是今天之前 这些模型大致如下: var Booking = new mongoose.Schema({ starts_at: { type: Date }, ends_at: { type: D

我试图在填充了引用的相应对象后查询引用数组。我有一种感觉,我要么没有使用正确的操作系统。。。或者我的方法不是正确的方式去做我需要做的事情

我举个例子来说明我到底想做什么。假设我有一组用户,他们可以有多个与他们相关联的预订文档。我需要找到所有用户谁住在某个位置,并有至少一个预订的开始日期是今天之前

这些模型大致如下:

var Booking = new mongoose.Schema({
    starts_at: { type: Date },
    ends_at:   { type: Date },
    //...
});

var User = new mongoose.Schema({
    name: String,
    location: String,
    bookings: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Booking' }],
    //...
});
User.find({ 'location': 'some place' })
    .populate('bookings')
    .where({ 'bookings.starts_at': { $lte: new Date() } })
    .exec(function(err, users) {
        //...
    });
我尝试的代码/查询大致如下:

var Booking = new mongoose.Schema({
    starts_at: { type: Date },
    ends_at:   { type: Date },
    //...
});

var User = new mongoose.Schema({
    name: String,
    location: String,
    bookings: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Booking' }],
    //...
});
User.find({ 'location': 'some place' })
    .populate('bookings')
    .where({ 'bookings.starts_at': { $lte: new Date() } })
    .exec(function(err, users) {
        //...
    });
任何帮助或见解都会很好

 User
     .find({ 'location': 'some place'})
     .populate('bookings', null, 'starts_at' : { $lte: new Date()})
     .exec(function(err, result) {
        if (!err) {

            console.log(result);
        } else {
           console.log(err)
        }
 });

请试试这个

可能是非常相似的重复,但是那里的方法会给我一个匹配预订的数组,因为我需要用户作为结果来处理,预订也不包含反向引用-该引用是初始/第一次查询在(用户)上执行的同一模型的一部分,因此该方法不幸不起作用:(谢谢,在最后一个括号周围添加缺少的括号后,这会按需要工作arg@Scott这不会排除今天之前至少没有一次预订的用户。谢谢@JohnnyHK,是的,我用
users=users.filter(函数(user){return user.bookings.length;}过滤掉这些用户
-除非您能建议一种我可以包含在DB查询中的方法?