Skip to content

CheetSheet

Connect to MongoDB instance using mongosh

To connect with the local database on a different port, use theĀ --portĀ option:

mongosh --port 23023

The following connects to the remote database onĀ mymongodb.example.com. it will ask you for password that you have to enter.

mongosh "mongodb://mymongodb.example.com:23023" --username shubham

Add new field in multiple documents

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  { upsert: false, multi: true }
)

~~In the above example last 2 fieldsĀ false, trueĀ specifies theĀ upsertĀ andĀ multiĀ flags.~~

Upsert: If set to true, creates a new document when no document matches the query criteria.

Multi: If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.

Add field based on condition:

db.your_collection.update(
  { "field.some_field": "condition" },
  { $set: {"new_field": 1} },
  { upsert: false, multi: true }
)

Update nested value/embeded document

Object:

{
  _id: ObjectId("5a7e395e20a31e44e0e7e284"),
  name: "foo",
  address: { street: "123", town: "bar" }
}

Using Dot Notation:

db.people.update({ }, { $set: { "address.street": "Main Street" } })

Rename field in collections

Rename one field

db.collection.updateMany({}, {$rename:{"oldField":"newField"}}, { upsert: false, multi: true })

Rename multiple Field

db.collection.updateMany({}, {$rename:{"old1":"new1", "old2":"new2"}}, { upsert: false, multi: true })

Rename Subfield

db.collection.updateMany({}, {$rename:{"field.oldSub":"field.newSub"}}, { upsert: false, multi: true })

Or to just update the docs which contain the property

db.collection.updateMany({ "field.oldSub": { $exists: true } }, {$rename:{"field.oldSub":"field.newSub"}}, { upsert: false, multi: true })
db.userMessage.updateMany({}, [{ $set: { "relationships.data.assignedto": "$attributes.assignedto" }}])
db.userMessage.updateMany({}, {$unset: { "attributes.assignedto" : 1 }} , { upsert: false, multi: true })