I wanner insert a var object as a new document after aggregation operation in MongoDB. I try this method:
var postUpvote = db.collection.aggregation([
{$limit:1}
]).toArray()
db.collection.aggregation([
...
...
]).insert(postUpvote[0])
But db.collection.aggregation(...).instert() is not a function. The second aggregation result is just one document. And I also try to use $addFields to insert the postUpvote to the document, but I can't take this inserted file out of the document into a new document.
{
"_id" : 1671,
"CommentUserId" : 1671,
"CommentUserUpVote" : 2644,
"PostUserId" : 1671,
"PostUserVote" : 2644
}
The PostUserId and PostUserVote is the var postUpvote. The result I want is
{
"_id" : 1671,
"CommentUserId" : 1671,
"CommentUserUpVote" : 2644,
}
{
"_id": //Whatever, I don't care
"PostUserId" : 1671,
"PostUserVote" : 2644
}
You can use $out aggregation as a last step in your pipeline. But it replaces the output collection completely if it already exists. If you want to avoid it try $merge aggregation. It can incorporate results (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) into an existing collection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.