在Mongodb中使用Condition进行文本搜索

时间:2017-09-14 13:28:12

标签: php mongodb

我有以下情况,我有一个集合“演示”,其中包含所有需要搜索的数据, 我用过文字搜索,

{
    "_id" : "1", 
    "type" : "Deal", 
    "description_title" : "New Deal for test", 
    "images" : "", 
    "start_date" : ISODate("2017-07-20T00:00:00.000+0000"), 
    "end_date" : ISODate("2017-10-30T11:59:00.000+0000"), 
    "status" : "1"
}
{
    "_id" : "2", 
    "type" : "Event", 
    "description_title" : "Event1 test", 
    "images" : "", 
    "end_date" : ISODate("2017-10-20T00:00:00.000+0000")
    "status" : "1"
}
{
    "_id" : "3", 
    "type" : "Text", 
    "description_title" : "test", 
    "images" : "",
    "status" : "1"
}
{
    "_id" : "4", 
    "type" : "Event", 
    "description_title" : "Event2 test", 
    "images" : "", 
    "end_date" : ISODate("2017-07-20T00:00:00.000+0000")
    "status" : "1"
}

在上面的集合中,我已经存储了所有需要使用文本搜索进行搜索的数据,但是当"type" : "Event"/"Deal"时它应该只返回Active Deals&需要搜索事件"Current date is less than end_date"。 在上面的集合当我搜索“测试”时,它应该返回,

{
    "_id" : "1", 
    "type" : "Deal", 
    "description_title" : "New Deal for test", 
    "images" : "", 
    "start_date" : ISODate("2017-07-20T00:00:00.000+0000"), 
    "end_date" : ISODate("2017-10-30T11:59:00.000+0000"), 
    "status" : "1"
}
{
    "_id" : "2", 
    "type" : "Event", 
    "description_title" : "Event1 test", 
    "images" : "", 
    "end_date" : ISODate("2017-10-20T00:00:00.000+0000")
    "status" : "1"
}
{
    "_id" : "3", 
    "type" : "Text", 
    "description_title" : "test", 
    "images" : "",
    "status" : "1"
}

我使用了以下查询,但它给了我所有结果,

    db.demo.find({'$text' : {'$search' => 'test'},'$or' : {
    {"type" : "Deal","end_date" : {'$gte' : ISODate("2017-09-14T11:59:00.000+0000") }},
    {"type" : "Text"},
{"type" : "Event","end_date" : {'$gte' : ISODate("2017-09-14T11:59:00.000+0000") }},
    }})

以上查询返回所有记录。 请帮助我实现我的成果。

0 个答案:

没有答案