MongoDB

Include or Exclude Fields to Return from a Query

The document can specify the inclusion of fields or the exclusion of fields. The specifications have the following forms.

  • Specify to include a field – 1 or TRUE
  • Specify to suppression/Exclude a field – 0 or FALSE
     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1 } )  

    In the result set, only the Name and Email fields and, by default, the _id field returns in the matching document (users). To remove the _id field from the results specifying it in exclusion.

     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1, _id:0 } )  
    MongoDB

    Remove Documents using Mongo Shell

    The db.collection.remove() method removes documents from a collection.

    • You can remove all documents from a collection.
    • Remove all documents that match a condition.
    • Or limit the operation to remove just a single document.
     db.users.remove({})  //Remove All Documents  
    db.users.remove({Name : "Himen Patel" }) //Remove Documents that Match a Condition
    db.users.remove({Name : "Himen Patel" },1 ) //Remove a Single Document that Matches a Condition
    MongoDB

    Upsert Option using Mongo Shell

    By default, if no document matches the update query, the update() method does nothing.

    However, by specifying upsert: true, the update() method either updates matching document or documents, or inserts a new document using the update specification if no matching document exists.

    The following operation either updates a matching document by replacing it with a new document or adds a new document if no matching document exists.

     > db.users.update(  
    ... { Name : "Hymen Patel" },
    ... {
    ... "Name": "Himen Paatel",
    ... "Email": "email2himen@dotnet-funda.com",
    ... "Address1": "1257 Commons Drive",
    ... "City": "Cooperstown",
    ... "Zip Code": "13326",
    ... "Country": "USA"
    ... },
    ... { upsert: true }
    ... )

    The update operation returns a WriteResult object which contains the status of the operation, including whether the db.collection.update() method modified an existing document or added a new document.

     WriteResult({  
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("561324b1b6bb748311f00aac")
    })

    Specify upsert: true for the update specific fields operation

     db.users.update(   
    { Name : "Himen Patel" },
    {$set :
    { "Email" : "admin@dotnet-fundamentals.com"} },
    {upsert: true}
    )
    MongoDB

    Replace a document using Mongo Shell

    The following operation replaces the document with item equal to “Himen Patel”. The newly replaced document will only contain the the _id field and the fields in the replacement document.

     > db.users.update(  
    ... { Name : "Himen Patel" },
    ... {
    ... "Name": "Himen Paatel",
    ... "Email": "email2himen@dotnet-funda.com",
    ... "Address1": "1257 Commons Drive",
    ... "City": "Cooperstown",
    ... "Zip Code": "13326",
    ... "Country": "USA"
    ... }
    ... )

    A successful update of the document returns the following object.

     WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })