[英]Outputting multiple documents results in merging to one document in jsPDF
[英]Merging MongoDB fields of documents into one document
我正在使用 MongoDB 2.6.6
我在 MongoDB 集合中有這些文檔,這是一個示例:
{ ..., "field3" : { "one" : [ ISODate("2014-03-18T05:47:33Z"),ISODate("2014-06-02T20:00:25Z") ] }, ...}
{ ..., "field3" : { "two" : [ ISODate("2014-03-18T05:47:33Z"),ISODate("2014-06-02T20:00:25Z") ] }, ...}
{ ..., "field3" : { "three" : [ ISODate("2014-03-18T05:47:39Z"),ISODate("2014-03-19T20:18:38Z") ] }, ... }
我想將這些文件合並在一個字段中。 例如,我希望新結果如下:
{ "field3", : { "all" : [ ISODate("2014-03-18T05:47:39Z"),ISODate("2014-03-19T20:18:38Z"),...... ] },}
我只是不確定如何得到這個結果!
在這里並沒有留下太多內容,但可以說您可以使用 mapReduce 獲得那種合並結果:
db.collection.mapReduce(
function() {
var field = this.field3;
Object.keys(field).forEach(function(key) {
field[key].forEach(function(date) {
emit( "field3", { "all": [date] } )
});
});
},
function (key,values) {
var result = { "all": [] };
values.forEach(function(value) {
value.all.forEach(function(date) {
result.all.push( date );
});
});
result.all.sort(function(a,b) { return a.valueOf()-b.valueOf() });
return result;
},
{ "out": { "inline": 1 } }
)
考慮到它對做事的限制,mapReduce 的輸出格式並不完全相同:
{
"results" : [
{
"_id" : "field3",
"value" : {
"all" : [
ISODate("2014-03-18T05:47:33Z"),
ISODate("2014-03-18T05:47:33Z"),
ISODate("2014-03-18T05:47:39Z"),
ISODate("2014-03-19T20:18:38Z"),
ISODate("2014-06-02T20:00:25Z"),
ISODate("2014-06-02T20:00:25Z")
]
}
}
],
"timeMillis" : 86,
"counts" : {
"input" : 3,
"emit" : 6,
"reduce" : 1,
"output" : 1
},
"ok" : 1
}
由於此處聚合到單個文檔中是相當隨意的,您幾乎可以爭辯說您只是在客戶端代碼中采用相同類型的方法。
無論如何,這只會對相對較小的數據集有用,並且對客戶端處理具有相同的限制。 超過了 MongoDB 的 16MB BSON 限制,但肯定會受到要消耗的內存的限制。
所以我認為你需要添加一個“查詢”參數,但你的問題並不是很清楚。 無論是使用 mapReduce 還是您的客戶端代碼,您基本上都需要按照這種過程將數組“混搭”在一起。
我個人會在這里使用客戶端代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.