Skip to content

Instantly share code, notes, and snippets.

@myctw
Last active February 8, 2022 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myctw/c7e5520163cb701311756e2c61d6a37c to your computer and use it in GitHub Desktop.
Save myctw/c7e5520163cb701311756e2c61d6a37c to your computer and use it in GitHub Desktop.

MongoDB Syntax Quick List

Get All Collection Name Operation

  • Way1
db.getCollectionNames()
  • Way2
db.getCollectionNames().forEach(function(coll_name) {
  print(coll_name);
  /* or other operation like..
  var coll = db.getCollection(coll_name);
  coll.reIndex();
  */

});

Index

  • Batch Drop
db.getCollection('ledger').dropIndexes(['idx_1', 'idx2'])
  • Batch Create
db.runCommand(
    {
      createIndexes: "wager",
      indexes: [
        {
          key: { field1: 1, field2: -1},
          name: "idx_1"
        },
        {
          key: { field3: -1, field4: 1},
          name: "idx_2"
        }
      ]
    }
  );

$merge

db.getCollection('ledger').aggregate([
    { $project: { _id: 0 } },
    { $merge: { into: 'ledger-to' } }
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment