Mongodb 在数组中搜索范围内的日期

Mongodb 在数组中搜索范围内的日期,mongodb,Mongodb,我是MongoDB的新手,请在提问时记住这一点。我有一个名为“documents”的集合,其中有一个名为attributes的数组,其中包含不同类型的键:值对 { "_id" : ObjectId("5d376c67f6c305c7571f7dd7"), "name" : "testContract.pdf", "fileType" : "pdf", "attributes" : [ { "abc"

我是MongoDB的新手,请在提问时记住这一点。我有一个名为“documents”的集合,其中有一个名为attributes的数组,其中包含不同类型的键:值对

{
    "_id" : ObjectId("5d376c67f6c305c7571f7dd7"),
    "name" : "testContract.pdf",
    "fileType" : "pdf",
    "attributes" : [
            {
                    "abc" : 1
            },
            {
                    "def" : ISODate("2012-12-01T08:00:00Z")
            },
            {
                    "ghi" : "test"
            }
    ]
}
{
    "_id" : ObjectId("5d376ca4f6c305c7571f7dd8"),
    "name" : "1099.pdf",
    "fileType" : "pdf",
    "attributes" : [
            {
                    "def" : ISODate("2012-06-03T07:00:00Z")
            },
            {
                    "ghi" : "taxes"
            }
    ]
}
我想做的是返回此集合中符合日期范围的一个或多个文档。例如,我可以通过以下查询返回文件类型为“pdf”的所有文档-->

但我想弄清楚的是,我是否可以在搜索字符串的同时成功地搜索包含不同数据类型(如范围内的日期)的数组。我还可以使用以下命令搜索属性数组-->

这里是一个例子,我正试图得到,但它不工作

db.documents.find({'attributes':$match:{[{'def':{$gte:new Date('2011-11-30')}}]}});

您可以使用
$gte
$lte
在日期范围内进行查询。它看起来像这样:

{'def': { $gte: qryDateFrom, $lte: qryDateTo }}
根据您使用的是聚合pipleline还是常规mongoose查询,您可以相应地应用它

例如,在包含字符串匹配的聚合中使用
$match
,如下所示:

$match: { 
            $and: [
                {'id': { $ne: req.user._id }},
                {'def': { $gte: qryDateFrom, $lte: qryDateTo }}
            ]
        }

您可以使用
$gte
$lte
在日期范围内进行查询。它看起来像这样:

{'def': { $gte: qryDateFrom, $lte: qryDateTo }}
根据您使用的是聚合pipleline还是常规mongoose查询,您可以相应地应用它

例如,在包含字符串匹配的聚合中使用
$match
,如下所示:

$match: { 
            $and: [
                {'id': { $ne: req.user._id }},
                {'def': { $gte: qryDateFrom, $lte: qryDateTo }}
            ]
        }

你在找这样的东西吗

根据以下文件:

{
    "_id" : ObjectId("5d38aad64850fbd5d13f14bd"),
    "name" : "testxx.pdf",
    "attributes" : [
        {
            "abc" : 1
        },
        {
            "def" : ISODate("2012-12-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}
{
    "_id" : ObjectId("5d38b0eae4adbe945b6cbb89"),
    "name" : "testyy.pdf",
    "attributes" : [
        {
            "abc" : 2
        },
        {
            "def" : ISODate("2013-12-01T08:00:00Z")
        },
        {
            "ghi" : "test1"
        }
    ]
}
{
    "_id" : ObjectId("5d38b12f21e647b8d384d841"),
    "name" : "testzz.pdf",
    "attributes" : [
        {
            "abc" : 3
        },
        {
            "def" : ISODate("2012-05-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}

查询def>2010/11/30-返回上述所有3个文档

db.chkdates.find({'attributes.def':{$gte:new Date(2010,11,30)}}).pretty()
添加另一个键/值对和范围如下所示:

db.chkdates.find({'attributes.def':{$gte:new Date(2011,12,12), 
                                    $lte:new Date(2012,10,12)},
'attributes.ghi':'test'}).pretty()
仅返回1个文档:

{
    "_id" : ObjectId("5d38b12f21e647b8d384d841"),
    "name" : "testzz.pdf",
    "attributes" : [
        {
            "abc" : 3
        },
        {
            "def" : ISODate("2012-05-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}


你在找这样的东西吗

根据以下文件:

{
    "_id" : ObjectId("5d38aad64850fbd5d13f14bd"),
    "name" : "testxx.pdf",
    "attributes" : [
        {
            "abc" : 1
        },
        {
            "def" : ISODate("2012-12-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}
{
    "_id" : ObjectId("5d38b0eae4adbe945b6cbb89"),
    "name" : "testyy.pdf",
    "attributes" : [
        {
            "abc" : 2
        },
        {
            "def" : ISODate("2013-12-01T08:00:00Z")
        },
        {
            "ghi" : "test1"
        }
    ]
}
{
    "_id" : ObjectId("5d38b12f21e647b8d384d841"),
    "name" : "testzz.pdf",
    "attributes" : [
        {
            "abc" : 3
        },
        {
            "def" : ISODate("2012-05-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}

查询def>2010/11/30-返回上述所有3个文档

db.chkdates.find({'attributes.def':{$gte:new Date(2010,11,30)}}).pretty()
添加另一个键/值对和范围如下所示:

db.chkdates.find({'attributes.def':{$gte:new Date(2011,12,12), 
                                    $lte:new Date(2012,10,12)},
'attributes.ghi':'test'}).pretty()
仅返回1个文档:

{
    "_id" : ObjectId("5d38b12f21e647b8d384d841"),
    "name" : "testzz.pdf",
    "attributes" : [
        {
            "abc" : 3
        },
        {
            "def" : ISODate("2012-05-01T08:00:00Z")
        },
        {
            "ghi" : "test"
        }
    ]
}


我熟悉$gte、$lte等,但我无法使其正常工作。您能提供一个示例查询吗?例如,我希望所有文档的“def”值都大于“11/30/2010”。感谢您的帮助,这并不完全是我想要的,但我非常感谢您的努力。我熟悉$gte、$lte等,但我无法实现这一点。您能提供一个示例查询吗?例如,我希望所有文档的“def”值大于“11/30/2010”。谢谢您的帮助,这并不完全是我想要的,但我感谢您的努力。是的,先生,这正是我想要的。谢谢你的帮助!是的,先生,正是我要找的。谢谢你的帮助!