Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store the aggregation result into another database

Tags:

mongodb

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
});
like image 294
newBike Avatar asked Sep 13 '25 14:09

newBike


2 Answers

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.

like image 165
chridam Avatar answered Sep 16 '25 04:09

chridam


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.

like image 42
Xavier Guihot Avatar answered Sep 16 '25 05:09

Xavier Guihot