好吧,我在mongodb上遇到了麻烦,来自MySQL,我遇到了麻烦
编写查询以获取我的数据。我的数据库如下所示:

opened: [
            {day: "Monday", from: 64800, till: 79200},
            {day: "Thuesday", from: 64800, till: 79200},
            {day: "Wednesday", from: 64800, till: 79200},
            {day: "Thursday", from: 64800, till: 79200},
            {day: "Friday", from: 64800, till: 79200},
            {day: "Saturday", from: 64800, till: 79200},
            {day: "Sunday", from: 64800, till: 79200}
        ]


我要完成的工作如下:
我有一个名为:currendTime的变量,并且我有一个以currentDay为全名的变量。因此变量currentDay包含“星期五”。

我想要实现的是搜索currentDay等于Friday并且当前时间高于“ from”且currentTime低于“ till”的日期

有人可以帮我吗?

更新资料
这是我到目前为止所拥有的:

db.collection('test', function(err, collection) {
    collection.find(
        {
            opened: {$and: [{$elemMatch: {day: weekDay}},
                            {from: {$lt: currentTime}},
                            {till: {$gt: currentTime}}]}
            }).toArray(function(err,items) {
            res.send(items);
        });

最佳答案

您需要组合所有要匹配的数组元素字段到$elemMatch对象中:

db.collection('test', function(err, collection) {
    collection.find({
        opened: {
            $elemMatch: {
                day: weekDay,
                from: {$lt: currentTime},
                till: {$gt: currentTime}
            }
        }
    }).toArray(function(err,items) {
        res.send(items);
    });


如果只需要匹配的元素,请在find调用中包含以下字段投影参数:

{ 'opened.$': 1 }

08-26 12:06