MongoDb commands

unpublished draft
MongoDb

Fundamental#

Connection String:

Backup and restore#

Backup
mongodump --host=127.0.0.1 --port=27017 --username=YourUserName --password YourUserPass --authenticationDatabase=admin --out=Physical_Folder --db=Database_To_Backup
Restore
mongorestore --host=127.0.0.1 --port=27017 -u YourUserName -p YourUserPass -d Database_To_Restore path_To_Backup_Physical_file

Collections indexes#

Get all index in a collection
db.<CollectionName>.getIndexes()
Create Index
db.<CollectionName>.createIndex({<PropertyOne>: 1, <PropertyTwo>: 1});
db.<CollectionName>.createIndex({<PropertyOne>: -1, <PropertyTwo>: 1}, {collation: {locale: "en", strength: 2}});
Drop Index
db.<CollectionName>.dropIndex(<IndexName>);

Profiler#

Profiler
db.setProfilingLevel(2);

MongoDb Query profiler:

Get to see the query infos
db.system.profile.find().sort({ ts: -1 });

Query#

Adhoc#

BinData(3, 'y7YnDN9ST0qP600BcYZQeQ==').toCSUUID();
CSUUID('00112233-4455-6677-8899-aabbccddeeff');

// Create random ObjectId
var mongoDummyObjectId = function (year, month, day) {
  var timestamp = ((new Date(year, month, day).getTime() / 1000) | 0).toString(16);
  return (
    timestamp +
    'xxxxxxxxxxxxxxxx'
      .replace(/[x]/g, function () {
        return ((Math.random() * 16) | 0).toString(16);
      })
      .toLowerCase()
  );
};

Filtering#

db.<CollectionName>.find({"_id" : ObjectId("5f745496782671aa39d05aa4")});
db.<CollectionName>.find({"CreatedTime" : {$gte : ISODate("2020-09-30T00:00:00.000Z")}});
db.<CollectionName>.find({"Description": { $regex: /KBHD$/ }}).limit(50);
db.<CollectionName>.find({ Description: { $regex: "^Good day mate.*" } })
db.<CollectionName>.find({$and: [{"PropertyOne" : "f799f66e-9110-45e0-b617-dae0e5321fe4"}, {"PropertyTwo" : {$gte : ISODate("2020-11-17T07:57:35.695Z")}}]});
db.<CollectionName>.find({"PropertyOne" : {"$exists" : false}});

var start = new ISODate("2021-02-23T00:00:00+07:00");
var end = new ISODate("2021-02-24T00:00:00+07:00");
db.<CollectionName>.find({"PropertyOne": {$gte: start, $lt: end}});

db.<CollectionName>.find({"PropertyOne": "E6D15DF5-0384-4145-A031-F54BF60E3B74"}).collation({locale: "en", strength: 2});
Nested Array Process
/*
  Dummys: [
    {
      CreatedDate,
      Message
    }
  ]
*/

db.<CollectionName>.aggregate()
    .match({"_id": ObjectId("xxx")})
    .project({_id:0, "Dummys": 1})
    .unwind("$Dummys")
    .match({"Dummys.CreatedDate": {"$gte" : ISODate("2021-08-14T11:14:52.446+07:00")}})
    .sort({"Dummys.Message": 1})
    .skip(10)
    .limit(25);

db.<CollectionName>.aggregate([
    {$match: {"_id": ObjectId("xxx")}},
    {$project: {_id:0, "Dummys": 1}},
    {$unwind: "$Dummys"},
    {$match: {"Dummys.CreatedDate": {"$gte" : ISODate("2021-08-14T11:14:52.446+07:00")}}},
    {$replaceRoot: { newRoot: "$Dummys" }},
    {$sort: {"Message": -1}},
    {$skip: 10},
    {$limit: 25}
])

db.<CollectionName>.aggregate([
    {$match: {"_id": ObjectId("xxx")}},
    {$project: {_id:0, "Dummys": {$filter: {
      input: "$Dummys",
      as: "item",
      cond: { $eq: ["$$item.Message", "This is dum 2"] }
    }}}},
])

db.UserRegisters.aggregate([
    {$match: {"_id": ObjectId("xxx")}},
    {$project: {_id:0, "Dummys": {
        $slice: [
            {$filter:
                {input: "$Dummys",
                as: "item",
                cond: { $gte: ["$$item.CreatedDate", ISODate("2021-08-14T11:47:33.045Z") ] }
                }}, 5, 10]}
    }},
])

Insert#

db.<CollectionName>.insert(
   [
    {
        "PropertyOne" : "2CF105DD-9FB7-4BE5-9CD1-54F2A2ACCAAE",
        "PropertyTwo" : "Mobile"
    },
    {
        "PropertyOne" : "2CF105DD-9FB7-4BE5-9CD1-54F2A2ACCAAE",
        "PropertyTwo" : "Mobile"
        "PropertyThree" : "😘 Success 😘"
    }
    ]
);

db.<CollectionName>.aggregate({
    $project: {
        PropertyOne: "$SomeSourceCollectionProperty",
        PropertyTwo: {$SomeSourceCollectionProperty: "some value"},
        PropertyThree: {$SomeSourceCollectionProperty: "some value"}
    }
},
{ $out: '<DestinationCollectionName>' });

Update#

db.<CollectionName>.update({}, {{"$set": {"PropertyOne": { "$concat": ["$PropertyTwo"]}}}}, {multi: true}) -- update a field by another field
db.<CollectionName>.updateMany({"PropertyOne": "442b9694-eae5-4571-bab3-da112c58e412"}, { $set: {"PropertyTwo" : "5f73fd7bba27c3dc4ed3b94b"}});
db.<CollectionName>.updateMany({$and: [
        {PropertyOne : "B1894BE7-0F99-413A-9D63-6A6E3CC43D5E"},
        {PropertyTwo : ISODate("2021-03-14T17:52:04.451+07:00")}
        ]
    }, { $set: {"PropertyThree" : "\nTesst\n\n Test "}},
    {collation: {locale: "en", strength: 2}});

db.<CollectionName>.update({"PropertyOne": ""}, {"$unset": {"PropertyTwo": ""}}, {multi: true});
db.<CollectionName>.aggregate(
    [
        { "$addFields": {
            "NewProperty": { "$concat": [ "$PropertyOne", " "] }
        }},
        { "$out": "<CollectionName>" }
    ]
);

db.<CollectionName>.aggregate(
    [
        { "$addFields": {
            "NewProperty": {"$add": ["$PropertyOne", 0]}
        }},
        { "$out": "<CollectionName>" }
    ]
);
Nested functional update
db.collection.aggregate(
  { $set: {
    { "service.apps.updates":
      { $function: {
          body: function(updates) {
            updates.sort((a, b) => a.date - b.date);
            return updates;
          },
          args: ["$service.apps.updates"],
          lang: "js"
      }}
    }
  }
)

Delete#

Delete by cursor and complex conditions
var deleteCur = db.<CollectionName>.aggregate([
    { $sort: { "PropertyOne": -1 } },
    {
      $group: {
        _id: { PropertyTwo: "$PropertyTwo", PropertyThree: "$PropertyThree" },
        _idsNeedsToBeDeleted: { $push: "$$ROOT._id" } // push all `_id`'s to an array
      }
    },
    /** Remove first element - which is removing a doc */
    {
      $project: {
        _id: 0,
        _idsNeedsToBeDeleted: { $slice: [ "$_idsNeedsToBeDeleted", 1, { $size: "$_idsNeedsToBeDeleted" } ] }
      }
    },
    {
      $unwind: "$_idsNeedsToBeDeleted" // Unwind `_idsNeedsToBeDeleted`
    },
    /** Group without a condition & push all `_idsNeedsToBeDeleted` fields to an array */
    {
      $group: { _id: "", _idsNeedsToBeDeleted: { $push: "$_idsNeedsToBeDeleted" } }
    },
    {$project : { _id : 0 }} // Optional stage
     /** At the end you'll have an [{ _idsNeedsToBeDeleted: [_ids] }] or [] */
  ]);

db.<CollectionName>.find({"_id": {$in: deleteCur.next()._idsNeedsToBeDeleted}});

Aggregate#

Aggregate for filter
var conversationCur = db.<CollectionName>.aggregate([
    {'$match': {'$and': [
        {"PropertyOne": {'$gte': ISODate("2021-07-25T10:00:00.000+07:00")}},
        {"PropertyOne": {'$lte': ISODate("2021-07-26T10:00:00.000+07:00")}}
    ]}},
    {'$project': {'_id': 1}},
    {'$group': { _id: '', _conversationId: {'$push': '$_id'}}}
    ]);

var ids = conversationCur.next()._conversationId;

db.<AnotherCollectionName>.find({'PropertyTwo': {$in: ids}})
   .sort({_id:-1})
   .limit(100);
Aggregate for joining
/*
  Conversations: 
    - _id
    - Name
    - createdTime

  Messages:
    - conversationId
    - senderId
    - content
*/
db.Conversations.aggregate([
  {
    $match: {
      $and: [
        { createdTime: { $gte: ISODate('2021-07-25T10:00:00.000+07:00') } },
        { createdTime: { $lte: ISODate('2021-07-26T10:00:00.000+07:00') } },
      ],
    },
  },
  {
    $lookup: {
      from: 'Messages', // <collection to join>
      localField: '_id', // <field from the input documents>
      foreignField: 'conversationId', // <field from the documents of the "from" collection>
      as: 'messages', // <output array field>, consider no new property
    },
  },
  { $unwind: '$messages' },
  { $sort: { 'messages.createdTime': 1 } },
  { $group: { _id: '$_id', rootDoc: { $first: '$$ROOT' }, sortedMessages: { $push: '$messages' } } },
  { $replaceRoot: { newRoot: { $mergeObjects: ['$rootDoc', { messages: '$sortedMessages' }] } } },
  //{'$project': {'messages': 0}}
]);

Khanh Nguyen

Web developer & .Net lover