I can store an aggregation result into another collection within the same database.
But how can I store the result into another database?
This is for copying a collection into another database:
use test1;
db["user_data"].find().forEach(
function(d){ db.getSiblingDB("test2")['user_data'].insert(d);
});
Aggregation function:
pipeline = [
{
'$group': {
'_id': {
'$year': '$birthday'
},
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'_id': 1
}
}, {
'$out': output_collection
}
];
cur = db[source_collection].runCommand('aggregate', {
pipeline: pipeline,
allowDiskUse: true
});
After running the aggregation to the output collection, you need to run another command that clones the collection to another database using db.cloneCollection()
as follows:
db.runCommand({
cloneCollection: "test.output_collection",
from: "mongodb.example.net:27017",
query: { active: true }
})
The above copies the output_collection
collection from the test
database on the server at mongodb.example.net
. The operation only copies documents that satisfy the query { active: true }
but the query arguments is optional. cloneCollection
always copies indexes.
Starting Mongo 4.2
, the new $merge
aggregation operator can be used to write the result of an aggregation pipeline to the specified collection within another database:
db.collection.aggregate([
// { $group: { "_id": { $year: "$birthday" }, "count": { $sum: 1 } } },
// { $sort: { "_id": 1 } },
{ $merge: { into: { db: "to", coll: "collection" } } }
])
Note that if the targeted collection already contains records, the $merge
operator comes with many options to specify how to merge inserted records conflicting with existing records.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With