Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
仅从Mongodb查询中提取字段_Mongodb - Fatal编程技术网

仅从Mongodb查询中提取字段

仅从Mongodb查询中提取字段,mongodb,Mongodb,我有这个收藏 db.Tratte.insert( { "TransportID":["9901","9903"], "StopsBy":[ {"Station":"Roma Termini", "StopsAt":ISODate("2016-01-01T09:05:00Z"), "LeavesAt":ISODate("2016-01-01T09:25:00Z")}, {"Station":"Napoli Centrale",

我有这个收藏

 db.Tratte.insert(
    {
    "TransportID":["9901","9903"],
    "StopsBy":[
     {"Station":"Roma Termini",
     "StopsAt":ISODate("2016-01-01T09:05:00Z"),
     "LeavesAt":ISODate("2016-01-01T09:25:00Z")},
      {"Station":"Napoli Centrale",
     "StopsAt":ISODate("2016-01-01T10:37:00Z"),
     "LeavesAt":ISODate("2016-01-01T10:52:00Z")},
     {"Station":"Salerno",
     "StopsAt":ISODate("2016-01-01T11:35:00Z")}]
    })
我想提取火车离开“罗马终点站”的日期(而且只提取日期)

我尝试了以下查询,但通过这种方式我获得了所有信息

db.Tratte.find({"TransportID":"9901"},
   { StopsBy: 
     { 
        $elemMatch: { "Station": "Roma Termini" } 
     } 
    }

)
如何仅提取与“Roma Termini”相关的“LeavesAt”字段? 感谢使用聚合查询-

db.Tratte.aggregate([
{$match: {"TransportID": "9901"}}, 
{$unwind: "$StopsBy"}, 
{$match: { "StopsBy.Station" : "Roma Termini" }}, 
{$project: {"StopsBy.LeavesAt": 1}} ])
试试这个

db.Tratte.find(
    {"TransportID":"9901"}, 
    {StopsBy: { $elemMatch: { "Station": "Roma Termini" } }, "StopsBy.LeavesAt": 1 }
) 

你需要使用点符号进入StopsBy文档的“内部”

你可能会在这里找到你想要的东西,可能我尝试使用投影并在查询末尾添加{“LeavesAt”:1}不会改变任何东西,如果我在没有elemMatch操作符的情况下编写查询并尝试执行{“LeavesAt”:1},我将获得所有的“LeavesAt”信息,但我只想要与我的请求相关的内容(在本例中为“Roma”)试试这个-
db.Tratte.find({“TransportID”:“9901”},{StopsBy:{$elemMatch:{“Station”:“Roma Termini”},{StopsBy.LeavesAt”:1})
。哦,天哪,我忘了准备StopsBy了!!非常感谢。通过这种方式,我获得了“StopsBy”:[“LeavesAt”:ISODate()。。。。有没有办法直接获取ISODate的值?@Alex请写为答案,我会批准它。它返回以下错误:Sintax错误:缺少:在属性id@(shell)之后:1:75