簡體   English   中英

如何在mongodb中的.aggregation之后插入一個數組作為新文檔?

[英]How to insert an array as a new document after the .aggregation in mongodb?

我想在 MongoDB 中的聚合操作后插入一個 var object 作為新文檔。 我試試這個方法:

    var postUpvote = db.collection.aggregation([
    {$limit:1}
    ]).toArray()

    db.collection.aggregation([
    ...
    ...
    ]).insert(postUpvote[0])

但是 db.collection.aggregation(...).instert() 不是 function。 第二個聚合結果只是一個文檔。 而且我還嘗試使用 $addFields 將 postUpvote 插入到文檔中,但是我無法將此插入的文件從文檔中取出到新文檔中。

{
    "_id" : 1671,
    "CommentUserId" : 1671,
    "CommentUserUpVote" : 2644,
    "PostUserId" : 1671,
    "PostUserVote" : 2644
}

PostUserId 和 PostUserVote 是 var postUpvote。 我想要的結果是

{
    "_id" : 1671,
    "CommentUserId" : 1671,
    "CommentUserUpVote" : 2644,
}
{
    "_id": //Whatever, I don't care
    "PostUserId" : 1671,
    "PostUserVote" : 2644
}

您可以使用$out聚合作為管道中的最后一步。 但如果 output 集合已經存在,它將完全替換它。 如果你想避免它嘗試$merge聚合。 它可以將結果(插入新文檔、合並文檔、替換文檔、保留現有文檔、操作失敗、使用自定義更新管道處理文檔)合並到現有集合中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM