我收集了一些推文,我試圖將根級別的轉推(類似於引用推文)輸出到新集合,以便稍後將它們與使用轉儲的原始集合合併,恢復)。 轉推狀態是tweet文檔中的一個子文檔,可能有多個推文轉發同一推文。 如何在根級上進行轉推,並添加一個名爲'retweeted_by'的數組,其中包含所有推特轉發的ID?mongodb:返回子文檔並跟蹤父代
請記住,我將推文ID用作主索引(_id),以避免在組合(mongorestore)集合時創建重複項。
我收藏有以下形式:
{
"_id" : "123456",
"other_fields1" : "values1",
"retweeted_status" : {
"retweet_id": "159753",
"other_fields2" : "values2",
}
}
理想的產量預計將看起來像:
{
"_id" : "159753",
"other_fields2" : "values2",
"retweeted_by" : [ "123456", "974631", "121212"]
}
編輯澄清:
子文檔中的字段(other_fields2 )是多個字段(〜28),並非全部存在於其他推文中
'db.collection.aggregate([{$組:{_id: 「$ retweeted_status.retweet_id」,retweeted_by :{$ push:「$ _id」}}}])' – felix
@felix謝謝,但這隻輸出retweeted_status的id,而不是retweedted_status的整個子文檔,在我的示例「other_fields2」中調用...我想在我需要使用$ replaceRoot將子文檔作爲newRoot,並以某種方式向其添加數組retweted_by –
add'other_fields2:{$ first:「$ retweeted_status.other_fields2」}'。請看[mongodb documentation $ group](https://docs.mongodb.com/manual/reference/operator/aggregation/group/) – felix