How do i merge two mongodb databases?

I am using mongoDB to store the data of a particular website. Since two of us are working, we are using our own computer for the job. Both the computer has a database website_data and a collection in that database webpages. Now for some analysis, and to plot graphs, I need the whole data in a single pc. How to combine the two databases? I thought of writing a script but I don't know how to connect to the other computer's database. Is there some database file which I can copy directly onto my pc?

asked Sep 17, 2015 at 3:47

How do i merge two mongodb databases?

Dibya Jyoti RoyDibya Jyoti Roy

1231 gold badge2 silver badges9 bronze badges

You can do this with the command line tools mongodump and mongorestore.

Use mongodump --db [dbname] on the source computer to export all collections of the database into files which are stored in the directory dump/[collection].bson. Copy the files to the target computer and then use mongorestore --db [dbname] [collection].bson to import the generated files into the consolidated database. The contents will be appended to the existing collections as if you would use the insert command.

When you want to do it like a senior sysadmin: both command line tools have command line options to perform said operations on a remote system and you can pipe the output of mongodump right into mongorestore, so when you want to show off you could do it with one console command from a remote system. But when this is a one time thing and command line trickery is not your passion, rather stick to files.

Alex

10.7k6 gold badges61 silver badges71 bronze badges

answered Sep 17, 2015 at 8:27

2

Docs HomeMongoDB Manual

Note

This page describes the $merge stage, which outputs the aggregation pipeline results to a collection. For the $mergeObjects operator, which merges documents into a single document, see $mergeObjects.

$merge

New in version 4.2.

Writes the results of the aggregation pipeline to a specified collection. The $merge operator must be the last stage in the pipeline.

The $merge stage:

  • Can output to a collection in the same or different database.

  • Starting in MongoDB 4.4:

    • $merge can output to the same collection that is being aggregated. For more information, see Output to the Same Collection that is Being Aggregated.

    • Pipelines with the $merge stage can run on replica set secondary nodes if all the nodes in cluster have featureCompatibilityVersion set to 4.4 or higher and the Read Preference allows secondary reads.

      • Read operations of the $merge statement are sent to secondary nodes, while the write operations occur only on the primary node.

      • Not all driver versions support targeting of $merge operations to replica set secondary nodes. Check your driver documentation to see when your driver added support for $merge read operations running on secondary nodes.

  • Creates a new collection if the output collection does not already exist.

  • 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.

  • Can output to a sharded collection. Input collection can also be sharded.

For a comparison with the $out stage which also outputs the aggregation results to a collection, see $merge and $out Comparison.

Note

On-Demand Materialized Views

$merge can incorporate the pipeline results into an existing output collection rather than perform a full replacement of the collection. This functionality allows users to create on-demand materialized views, where the content of the output collection is incrementally updated when the pipeline is run.

For more information on this use case, see On-Demand Materialized Views as well as the examples on this page.

Materialized views are separate from read-only views. For information on creating read-only views, see read-only views.

$merge has the following syntax:

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }

For example:

{ $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

If using all default options for $merge, including writing to a collection in the same database, you can use the simplified form:

{ $merge: } // Output collection is in the same database

The $merge takes a document with the following fields:

Field

Description

into

The output collection. Specify either:

  • The collection name as a string to output to a collection in the same database where the aggregation is run. For example:

    into: "myOutput"

  • The database and collection name in a document to output to a collection in the specified database. For example:

    into: { db:"myDB", coll:"myOutput" }

Note

  • If the output collection does not exist, $merge creates the collection:

    • For a replica set or a standalone, if the output database does not exist, $merge also creates the database.

    • For a sharded cluster, the specified output database must already exist.

  • The output collection can be a sharded collection.

on

Optional. Field or fields that act as a unique identifier for a document. The identifier determines if a results document matches an existing document in the output collection. Specify either:

  • A single field name as a string. For example:

    on: "_id"

  • A combination of fields in an array. For example:

    on: [ "date", "customerId" ]

    The order of the fields in the array does not matter, and you cannot specify the same field multiple times.

For the specified field or fields:

  • The aggregation results documents must contain the field(s) specified in the on, unless the on field is the _id field. If the _id field is missing from a results document, MongoDB adds it automatically.

  • The specified field or fields cannot contain a null or an array value.

$merge requires a unique, index with keys that correspond to the on identifier fields. Although the order of the index key specification does not matter, the unique index must only contain the on fields as its keys.

  • The index must also have the same collation as the aggregation's collation.

  • The unique index can be a sparse index.

  • The unique index cannot be a partial index.

  • For output collections that already exist, the corresponding index must already exist.

The default value for on depends on the output collection:

  • If the output collection does not exist, the on identifier must be and defaults to the _id field. The corresponding unique _id index is automatically created.

    Tip

To use a different on identifier field(s) for a collection that does not exist, you can create the collection first by creating a unique index on the desired field(s). See the section on non-existent output collection for an example.

  • If the existing output collection is unsharded, the on identifier defaults to the _id field.

  • If the existing output collection is a sharded collection, the on identifier defaults to all the shard key fields and the _id field. If specifying a different on identifier, the on must contain all the shard key fields.

  • whenMatched

    Optional. The behavior of $merge if a result document and an existing document in the collection have the same value for the specified on field(s).

    You can specify either:

    • One of the pre-defined action strings:

      Action

      Description

      "replace"

      Replace the existing document in the output collection with the matching results document.

      When performing a replace, the replacement document cannot result in a modification of the _id value or, if the output collection is sharded, the shard key value. Otherwise, the operation generates an error.

      Tip

      To avoid this error, if the on field does not include the _id field, remove the _id field in the aggregation results to avoid the error, such as with a preceding $unset stage, and so on.

    "keepExisting"

    Keep the existing document in the output collection.

    "merge" (Default)

    Merge the matching documents (similar to the $mergeObjects operator).

    • If the results document contains fields not in the existing document, add these new fields to the existing document.

    • If the results document contains fields in the existing document, replace the existing field values with those from the results document.

    For example, if the output collection has the document:

    { _id: 1, a: 1, b: 1 }

    And the aggregation results has the document:

    { _id: 1, b: 5, z: 1 }

    Then, the merged document is:

    { _id: 1, a: 1, b: 5, z: 1 }

    When performing a merge, the merged document cannot result in a modification of the _id value or, if the output collection is sharded, the shard key value. Otherwise, the operation generates an error.

    Tip

    To avoid this error, if the on field does not include the _id field, remove the _id field in the aggregation results to avoid the error, such as with a preceding $unset stage, and so on.

    How do I merge two MongoDB collections?

    For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents.

    How can I combine data from multiple collections into one collection?

    You need to have some key in both collections that you can use as an _id..
    mongodump collection1..
    collection2. rename(collection1).
    mongorestore..

    How does MongoDB connect to another database?

    According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.

    How do I merge two arrays in MongoDB aggregate?

    MongoDB provides different types of array expression operators that are used in the aggregation pipeline stages and $concatArrays operator is one of them. This operator is used to concatenate two or more arrays and return a concatenated array.