How to delete multiple ids in MongoDB?

Chandu yadav
Published on 12-Apr-2019 11:02:35
To delete multiple ids in MongoDB, you can use $in operator. Following is the syntaxdb.yourCollectionName.remove( { _id : { $in: [yourObjectId1, yourObjectId2, yourObjectId3)] } } );Let us create a collection with documents> db.deleteMultipleIdsDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7d6a629b87623db1b19") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Robert", "ClientAge":28}); {    "acknowledged" ... Read More

How to insert a document with date in MongoDB?

Arjun Thakur
Published on 12-Apr-2019 11:01:00
To insert a document with date in MongoDB, use Date(). Following is the syntax“yourFieldName”:new Date(yourDateValue);Let us create a collection with documents. Following is the query>db.insertDocumentWithDateDemo.insertOne({"UserName":"Larry", "UserMessage":"Hi", "UserMessagePostDate":new Date("2012-09-24")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cca58a629b87623db1b16") } >db.insertDocumentWithDateDemo.insertOne({"UserName":"Chris", "UserMessage":"Hello", "UserMessagePostDate":new Date("2015-12-31")}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to connect to my MongoDB table by command line?

Ankith Reddy
Published on 12-Apr-2019 10:58:50
In order to connect to my table by command line, you need to use db commanddb.yourCollectionName.find();Let’s say we have a database “sample” with some collections. First check the current database> use sample; switched to db sample > db; Sample Now we have reached the database sample. The database “sample” is ... Read More

Handling optional/empty data in MongoDB?

George John
Published on 12-Apr-2019 10:57:35
In order to handle empty data, you can use $ne operator. Let us create a collection with documents. Following is the query>db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cbd5ca629b87623db1b12") } >db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cbd6ba629b87623db1b13") } > db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John"}); {    "acknowledged" ... Read More

Change collection name in MongoDB?

Chandu yadav
Published on 12-Apr-2019 10:55:42
Use use renameCollection() to change collection name in MongoDB. Following is the syntaxdb.yourOldCollectionName.renameCollection("yourNewCollectionName");Let us create a collection with documents. Following is the query> db.savingInformationDemo.insertOne({"StudentName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cb44da629b87623db1b07") } > db.savingInformationDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cb45da629b87623db1b08") } > db.savingInformationDemo.insertOne({"StudentName":"Mike"}); {   ... Read More

How to loop through collections with a cursor in MongoDB?

Arjun Thakur
Published on 12-Apr-2019 10:53:48
Following is the syntax to loop through collections with cursorvar anyVariableName1; var anyVariableName2= db.yourCollectionName.find(); while(yourVariableName2.hasNext()) {    yourVariableName1= yourVariableName2.next(); printjson(yourVariableName1); };Let us create a collection with documents. Following is the query> db.loopThroughCollectionDemo.insertOne({"StudentName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ca81f2d6669774125247f") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); {    "acknowledged" : ... Read More

How to remove all documents from a collection except a single document in MongoDB?

Ankith Reddy
Published on 12-Apr-2019 10:52:11
To remove all documents from a collection except a single document in MongoDB, use remove() based on some condition. Let us create a collection with documents. Following is the query>db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c9de42d66697741252478") } >db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Mike", "StudentAge":21, "StudentCountryName":"US"}); {    "acknowledged" : true,   ... Read More

How to compare field values in MongoDB?

George John
Published on 12-Apr-2019 10:50:29
You can use $where operator to compare field values in MongoDB. Let us first create a collection with documents> db.comparingFieldDemo.insertOne({"Value1":30, "Value2":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c99ed2d6669774125246e") } > db.comparingFieldDemo.insertOne({"Value1":60, "Value2":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c99f62d6669774125246f") } > db.comparingFieldDemo.insertOne({"Value1":160, "Value2":190}); {    "acknowledged" ... Read More

How to find exact Array Match with values in different order using MongoDB?

Chandu yadav
Published on 12-Apr-2019 10:48:48
To find exact array match with values in different order, you can use $all operator. Let us create a collection with documents. Following is the query>db.exactMatchArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentGameScores":[45, 78, 98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c94702d6669774125246c") } >db.exactMatchArrayDemo.insertOne({"StudentName":"Chris", "StudentAge":23, "StudentGameScores":[45, 78]}); {    "acknowledged" : true,   ... Read More

How to make a call in android

Vrundesha Joshi
Published on 12-Apr-2019 10:33:52
This example demonstrate about How to make a call in androidStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.<?xml version = "1.0" encoding = "utf-8"?> ... Read More
Advertisements