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
带有额外信息的mongodb聚合_Mongodb_Mongodb Aggregation - Fatal编程技术网

带有额外信息的mongodb聚合

带有额外信息的mongodb聚合,mongodb,mongodb-aggregation,Mongodb,Mongodb Aggregation,我有一个mongo集合,其中包含以下文档: { "_id" : ObjectId("57697321c22d3917acd66513"), "parent" : "AlphaNumericID", "signature" : "AnotherAlphaNumericID", "price" : 1638, "url" : "http://www.thecompany.com/path/to/page1", "date" : ISODate("201

我有一个mongo集合,其中包含以下文档:

{
    "_id" : ObjectId("57697321c22d3917acd66513"),
    "parent" : "AlphaNumericID",
    "signature" : "AnotherAlphaNumericID",
    "price" : 1638,
    "url" : "http://www.thecompany.com/path/to/page1",
    "date" : ISODate("2016-06-21T17:02:20.352Z"),
    "valid" : true
}
我想做的是运行一个查询,在签名字段上分组,返回最小和最大价格以及相应的url:

{
        "signature" : "AnotherAlphaNumericID",  
        "min_price" : 1504,
        "min_rent_listing" : "http://www.thecompany.com/path/to/page1",
        "max_price" : 1737,
        "max_price_listing" : "http://www.thecompany.com/path/to/page2",
}
$signature
字段上运行
$group
以获得
$min
$max
是直接的,但是为了获得实际的URL,我将查询分为2个部分,第一个查询使用
$signature
返回一个文档排序列表,价格从最小到最大,然后(在python代码中)取第一个和最后一个元素。这很好,但是如果有一个查询就好了

想法

p、 美国


还“玩弄”了运行一个最小值查询和一个最大值查询,并“压缩”结果。

您可以在
$group
$project
的帮助下玩一个小把戏。假设数据集是

{ 
    "_id" : ObjectId("57db28dc705af235a826873a"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 1638.0, 
    "url" : "http://www.thecompany.com/path/to/page1", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873b"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 168.0, 
    "url" : "http://www.thecompany.com/path/to/page2", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873c"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 163.0, 
    "url" : "http://www.thecompany.com/path/to/page3", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873d"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 1680.0, 
    "url" : "http://www.thecompany.com/path/to/page4", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
尝试在shell中执行以下查询

db.collection.aggregate([
   {$sort:{price:1}},
   {$group:{
       _id:"$signature", 
       _first:{$first:"$url"},
       _last:{$last:"$url"}, 
       _min:{$first:"$price"}, 
       _max:{$last:"$price"}}
   },
   {$project:{
     _id:0, 
     min:{
       url:"$_first", 
       price:"$_min"}, 
     max:{
       url:"$_last", 
       price:"$_max"}}
   }
])
输出将包含最低/最高价格和相应的url

{ 
    "min" : {
        "url" : "http://www.thecompany.com/path/to/page3", 
        "price" : 163.0
    }, 
    "max" : {
        "url" : "http://www.thecompany.com/path/to/page4", 
        "price" : 1680.0
    }
}
我改变了原来的答案:
\u min:{$min:$price},
-->使用
$first
\u max:{$max:$price}
-->使用
$last


原因:我们进入管道时,价格呈上升趋势。默认情况下,第一条记录是最小值,最后一条记录是最大值。

您可以在
$group
$project
的帮助下玩把戏。假设数据集是

{ 
    "_id" : ObjectId("57db28dc705af235a826873a"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 1638.0, 
    "url" : "http://www.thecompany.com/path/to/page1", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873b"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 168.0, 
    "url" : "http://www.thecompany.com/path/to/page2", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873c"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 163.0, 
    "url" : "http://www.thecompany.com/path/to/page3", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
{ 
    "_id" : ObjectId("57db28dc705af235a826873d"), 
    "parent" : "AlphaNumericID", 
    "signature" : "AnotherAlphaNumericID", 
    "price" : 1680.0, 
    "url" : "http://www.thecompany.com/path/to/page4", 
    "date" : ISODate("2016-06-21T17:02:20.352+0000"), 
    "valid" : true
}
尝试在shell中执行以下查询

db.collection.aggregate([
   {$sort:{price:1}},
   {$group:{
       _id:"$signature", 
       _first:{$first:"$url"},
       _last:{$last:"$url"}, 
       _min:{$first:"$price"}, 
       _max:{$last:"$price"}}
   },
   {$project:{
     _id:0, 
     min:{
       url:"$_first", 
       price:"$_min"}, 
     max:{
       url:"$_last", 
       price:"$_max"}}
   }
])
输出将包含最低/最高价格和相应的url

{ 
    "min" : {
        "url" : "http://www.thecompany.com/path/to/page3", 
        "price" : 163.0
    }, 
    "max" : {
        "url" : "http://www.thecompany.com/path/to/page4", 
        "price" : 1680.0
    }
}
我改变了原来的答案:
\u min:{$min:$price},
-->使用
$first
\u max:{$max:$price}
-->使用
$last


原因:我们进入管道时,价格呈上升趋势。默认情况下,第一条记录是最小值,最后一条记录是最大值。

问题:是否有办法运行查询,使其返回最小值、最大值和最新值?在您的回答中,first/last=min/max,因为我们按价格排序:1。我们可以把结果用“latest”来“充实”它吗?或者我必须坚持两个查询,其中第一个查询是上面的,另一个查询是按日期排序的:-1,然后取第一个结果吗?好的,找到它的最好方法是尝试一下。猜一猜不是在一个查询中,因为我需要按min/max的价格排序。对于latest,我必须按日期排序。排序不能是“连续的”,而是独立的……问题:有没有办法运行查询,使其返回最小、最大和最新的值?在您的回答中,first/last=min/max,因为我们按价格排序:1。我们可以把结果用“latest”来“充实”它吗?或者我必须坚持两个查询,其中第一个查询是上面的,另一个查询是按日期排序的:-1,然后取第一个结果吗?好的,找到它的最好方法是尝试一下。猜一猜不是在一个查询中,因为我需要按min/max的价格排序。对于latest,我必须按日期排序。排序不能是“连续的”,而是独立的。。。