Found 1349 Articles for MongoDB

Upsert in MongoDB while using custom _id values to insert a document if it does not exist?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

509 Views

You need to use insert() for this. Whenever you insert custom _id values and the document already exist with the custom _id value then an error is visible. Let us first create a collection with documents. Under this, we tried adding the same document again and this resulted in an error> db.customIdDemo.insert({"_id":1, "StudentName":"John"}); WriteResult({ "nInserted" : 1 }) > db.customIdDemo.insert({"_id":1, "StudentName":"Carol"}); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate key error collection: admin.customIdDemo index: _id_ dup key: { : 1.0 }"    } }) > db.customIdDemo.insert({"_id":2, "StudentName":"Carol"}); ... Read More

How can I to know if my database MongoDB is 64 bits?

George John
Updated on 30-Jul-2019 22:30:25

96 Views

You can use buidInfo along with runCommand to check MongoDB for 32 bits or 64 bits. First switch your database to admin. Following is the syntaxuse adminAfter that use the following syntax to know if my server runs MongoDB 64 bits or notdb.runCommand(buildInfo)Now execute the above syntax> use admin switched to db admin > db.runCommand("buildInfo");Following is the output displaying MongoDB is 64 bits{    "version" : "4.0.5",    "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412",    "targetMinOS" : "Windows 7/Windows Server 2008 R2",    "modules" : [ ],    "allocator" : "tcmalloc",    "javascriptEngine" : "mozjs",    "sysInfo" : "deprecated",    "versionArray" : [ ... Read More

Access objects from the nested objects structure in MongoDB

Chandu yadav
Updated on 30-Jul-2019 22:30:25

606 Views

Access objects using dot notation. Let us first create a collection with documents> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], ... "StudentCountryName" : [ "US" ], ... "StudentCoreSubject" : [ "C", "Java" ], ... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99dfc2863d6ffd454bb650") }Following is the query to display all documents from a collection with the help of find() method> db.nestedObjectDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c99dfc2863d6ffd454bb650"),    "Student" : {       "StudentDetails" : ... Read More

Search multiple fields for multiple values in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

521 Views

To search multiple fields for multiple values in MongoDB, you can use $text and $search operator. Let us first create a collection with documents>db.searchMultipleFieldsDemo.insertOne({"_id":100, "FirstSubject":"Java", "SecondSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : 100 } >db.searchMultipleFieldsDemo.insertOne({"_id":101, "FirstSubject":"MongoDB", "SecondSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : 101 } >db.searchMultipleFieldsDemo.insertOne({"_id":102, "FirstSubject":"MySQL", "SecondSubject":"Java"}); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method> db.searchMultipleFieldsDemo.find().pretty();This will produce the following output{ "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" } { ... Read More

Find all the non-distinct values of a field in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

768 Views

Use aggregate() method to get all the non-distinct values of a field. Let us first create a collection with documents> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":25}); ... Read More

How to create an index with MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

123 Views

To create an index in MongoDB, use the ensureIndex() method. Let us first create a collection using following query> db.createCollection(&qu/ot;creatingUniqueIndexDemo"); { "ok" : 1 }Following is the query to create an index on the above collection:> db.creatingUniqueIndexDemo.ensureIndex({"UserCountryName":1}, {unique:true}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Following is the query to insert some documents in the above collection>db.creatingUniqueIndexDemo.insertOne({"UserName":"John", "UserAge":21, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9947bd330fd0aa0d2fe4d8") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Mike", "UserAge":23, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9947c9330fd0aa0d2fe4d9") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"US"}); 2019-03-26T02:57:52.670+0530 E QUERY [js] WriteError: ... Read More

Drop all indexes from all the collections in a MongoDB database using the command line?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

190 Views

Following is the syntax to drop all indexes from all collections in a MongoDB database using command linedb.getCollectionNames().forEach(function(yourVariableName) {    db.runCommand({dropIndexes: yourVariableName, index: "*"}); });The above syntax will drop all indexes except _id.Let us check the current database. Following is the query> dbThis will produce the following outputTestFollowing is the query to let us show some indexes from a collection before dropping indexes> db.indexingDemo.getIndexes();This will produce the following output[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_", ... Read More

Get all the MongoDB documents but not the ones with two given criteria’s?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

68 Views

To get all the MongoDB documents with some given criteria’s, following any of the below given casesCase 1 Following is the query to get all the documents without a single criterion using $ne operatordb.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();Case 2 Following is the query to get all the documents without two given criterions using $nin operatordb.yourCollectionName.find({yourFieldName:{$nin:["yourValue1", "yourValue2"]}}).pretty();Let us first create a collection. Following is the query to create a collection with documents>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry", "StudentSubjectName":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris", "StudentSubjectName":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert", "StudentSubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4") } ... Read More

Return True if a document exists in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

Let us first create a collection. Following is the query to create a collection with documents> db.documentExistsOrNotDemo.insertOne({"UserId":101, "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932bd330fd0aa0d2fe4cf") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932c6330fd0aa0d2fe4d0") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932ce330fd0aa0d2fe4d1") }Following is the query to display all the documents from a collection with the help of find() method> db.documentExistsOrNotDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9932bd330fd0aa0d2fe4cf"),    "UserId" : 101,    "UserName" : "John" } {    "_id" : ObjectId("5c9932c6330fd0aa0d2fe4d0"),    "UserId" : 102,   ... Read More

Clearing items in a nested MongoDB array?

George John
Updated on 30-Jul-2019 22:30:25

84 Views

To clear items in a nested array, use the $set operator. Let us first create a collection. Following is the query to create a collection with documents> db.clearingItemsInNestedArrayDemo.insertOne( { ... ...    "StudentName" : "John", ...    "StudentDetails" : [ ...       { ...          "ProjectName" : "Online Banking", ...          "ProjectDetails" : [ ...             { ...                "TechnologyUsed" : "Java", ...                "TeamSize":5 ...             }, ... ... ... Read More

Advertisements