Backup and upgrade MongoDB databases

I have a web application using MongoDB (Version 2.6.12), hosted in a (Ubuntu 16.04) server of DigitalOcean.

I like to use Robo 3T to connect to the remote database and do simple queries.

Sometimes I backup the database with mongodump, and mostly rely on the weekly auto server snapshot provided by DigitalOcean.

Now, I need to undertake queries containing like $lookup, they told me that MongoDB Version 2.16.12 does not support that. So I need to seriously backup my database and use a more recent MongoDB. I still want to keep using Robo3T to do queries to production database (by preference) or backup database (if it is updated very often, e.g., every day).

I have several questions:

1) I would prefer to have a more regular auto-backup (e.g., every day) of the database. Which way is recommended? (Additionally, it seems that Atlas does NOT support Cloud Provider Snapshots for clusters served on DigitalOcean?)

2) If I buy a new server hosted on Azure and install MongoDB 4.2, and copy the whole database by mongodump to the new server, will it work?

Go to Source
Author: SoftTimur

mongodump and restore in single command

How to perform mongo DB dump and restore in a single command, by providing source and sink DB and collection name separately?

I understand we can do mongodump and then mongorestore but if I need to this on an intermediate machine, this will mean I will need to wait for mongodump to complete before I can start mongorestore manually again. Is there a way to do it in a single command?

Go to Source
Author: Shubham A.

MongoDB query to try multiple cases using aggregate, $group, $switch…returning error Unrecognized pipeline stage name: ‘$switch’

MongoDB query to try multiple cases using aggregate, $group, $switch…returning error Unrecognized pipeline stage name: ‘$switch’

Using MongoDB aggregate to see if a first field and/or a second field have value(s), and if so, to increment the NumberMatches by either 1 or 2 (depending on whether one or both exampleA and exampleB have values), and to increment MatchesTotal by the values of exampleA/exampleB. I am using Mongo’s $switch, as with this example from Stack Overflow, and $group.

However, I am getting the error message Unrecognized pipeline stage name: '$switch'. Right now the $switch is outside of $group. Previously I had tried using the $switch within MongoDB $group to try the query, but got another error message unknown group operator '$switch'. How can I use $switch and $group together in order to do this incrementation/sum ternary query in MongoDB?

$switch query with $group inside:

db.examples.aggregate([
  {
    "$match": {
      $or : [
        { 
          $and : [ 
            {"exampleField.exampleA": {$type: 16}},
            {"exampleField.exampleB": { $type: 16 }}
          ]
        },
        { 
          "exampleField.exampleA": {$type: 16}
        },
        { 
          "exampleField.exampleB": {$type: 16}
        }               
      ]
    }
  },    
  {
    $switch: {
      "branches": [
        { "case": 
          { "$eq": [ "$exampleField.exampleA", null ] }, 
          "then": { 
            $group: {
              _id: null,
              NumberMatches: { $sum: 1 },
              MatchesTotal: { $sum: "exampleField.exampleB" }
            }
          }
        },
        { "case": { 
          "$eq": [ "exampleField.exampleB", null ] }, 
          "then": {     
            $group: {
              _id: null,
              NumberMatches: { $sum: 1 },
              MatchesTotal: { $sum: "$exampleField.exampleA" }
            } 
          }
        }
      ],
      "default": {     
        $group: {
          _id: null,
          NumberMatches: { $sum: 2 },
          MatchesTotal: { $sum: "$exampleField.exampleA" + "exampleField.exampleB" }
        } 
      }      
    }     
  },  
]).forEach( function(myDoc) { print( myDoc) } );

Alternate $group query with $switch inside (also didn’t work):

$group: {
  _id: null,

  NumberMatches: {
      $switch: {
          "branches": [
              { "case": { "$eq": [ "$exampleField.exampleA", null ] }, "then": { $sum: 1 } },
              { "case": { "$eq": [ "exampleField.exampleB", null ] }, "then": { $sum: 1 } }
            ],
        "default": { $sum: 2 }

      }

  },      
  MatchesTotal: { $sum: "$exampleField.exampleA" + "$exampleField.exampleB" }
}

Go to Source
Author: maudulus