Found 1349 Articles for MongoDB

Update Array element in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

165 Views

Use $addToSet operator to update array element. Let us first create a collection with documents −> db.updateArrayDemo.insertOne( ...    { ... ...       "ClientDetails" : [ ...          { ...             "ClientName" : "John", ...             "DeveloperDetails" : [ ] ...          }, ...          { ...             "ClientName" : "Larry", ...             "DeveloperDetails" : [ ] ...          } ...       ] ... ... Read More

Escaping quotes while inserting records in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

854 Views

The double quotes have unicode which has the value \u0022. Let us first create a collection with documents −> db.escapingQuotesDemo.insert({ "StudentFullName": "John \u0022 Smith" }); WriteResult({ "nInserted" : 1 }) > db.escapingQuotesDemo.insert({ "StudentFullName": "David \u0022 Miller" }); WriteResult({ "nInserted" : 1 }) > db.escapingQuotesDemo.insert({ "StudentFullName": "John \u0022 Doe" }); WriteResult({ "nInserted" : 1 }) > db.escapingQuotesDemo.insert({ "StudentFullName": "Carol \u0022 Taylor" }); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method −> db.escapingQuotesDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccf42e2dceb9a92e6aa195b"),    "StudentFullName" : "John \" Smith" ... Read More

Implement multiple conditions in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

197 Views

Let us first create a collection with documents −> db.multipleConditionDemo.insertOne({"_id":1, "Name":"John"}); { "acknowledged" : true, "insertedId" : 1 } > db.multipleConditionDemo.insertOne({"_id":2, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : 2 } > db.multipleConditionDemo.insertOne({"_id":3, "Name":"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > db.multipleConditionDemo.insertOne({"_id":4, "Name":"David"}); { "acknowledged" : true, "insertedId" : 4 }Following is the query to display all documents from a collection with the help of find() method −> db.multipleConditionDemo.find().pretty();This will produce the following output −{ "_id" : 1, "Name" : "John" } { "_id" : 2, "Name" : "Carol" } { "_id" : 3, "Name" : "Sam" } { ... Read More

Is it possible to achieve a slice chain in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

68 Views

Yes, you can achieve this using aggregate framework. Let us first create a collection with documents −> db.sliceOfSliceDemo.insertOne( ...    { ...       "Name": "John", ...       "Details": [["First 1:1", "First 1:2"], ["second 2:1", "Second 2:2"]] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf3fcfdceb9a92e6aa195a") }Following is the query to display all documents from a collection with the help of find() method −> db.sliceOfSliceDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccf3fcfdceb9a92e6aa195a"),    "Name" : "John",    "Details" : [       [          "First ... Read More

How to prepend string to entire column in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

122 Views

Prepend string to entire column in MongoDB using aggregate framework. Let us first create a collection with documents −> db.prependDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf3bcedceb9a92e6aa1955") } > db.prependDemo.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf3bd3dceb9a92e6aa1956") } > db.prependDemo.insertOne({"StudentFirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf3bd8dceb9a92e6aa1957") }Following is the query to display all documents from a collection with the help of find() method −> db.prependDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ccf3bcedceb9a92e6aa1955"), "StudentFirstName" : "John" } {    "_id" : ObjectId("5ccf3bd3dceb9a92e6aa1956"),    "StudentFirstName" : "Chris" } {    "_id" : ObjectId("5ccf3bd8dceb9a92e6aa1957"), ... Read More

How to perform intersection of sets between the documents in a single collection in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

101 Views

You can use $setIntersection for this. Let us first create a collection with documents −> db.setInterSectionDemo.insertOne( ...    {"_id":101, "Value1":[55, 67, 89]} ... ); { "acknowledged" : true, "insertedId" : 101 } > db.setInterSectionDemo.insertOne( ...    {"_id":102, "Value2":[90, 45, 55]} ... ); { "acknowledged" : true, "insertedId" : 102 } > db.setInterSectionDemo.insertOne( ...    {"_id":103, "Value3":[92, 67, 45]} ... ); { "acknowledged" : true, "insertedId" : 103 }Following is the query to display all documents from a collection with the help of find() method −> db.setInterSectionDemo.find().pretty();This will produce the following output −{ "_id" : 101, "Value1" : [ 55, 67, ... Read More

Querying with MongoDB subelement?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

119 Views

You can use positional operator $ for this. Let us first create a collection with documents −> db.subElementQueryingDemo.insertOne( ...    { ...       "ClientName":"Chris", ...       "Status": [ { "isMarried": true }, { "isMarried": false } ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf28c9dceb9a92e6aa1953") }Following is the query to display all documents from a collection with the help of find() method −> db.subElementQueryingDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccf28c9dceb9a92e6aa1953"),    "ClientName" : "Chris",    "Status" : [       {         ... Read More

How to find datatype of all the fields in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

3K+ Views

Use typeof to find datatype of all the fields −typeof db.yourCollectionName.findOne().yourFieldName;Let us first create a collection with documents −> db.findDataTypeDemo.insertOne({"ClientName":"Chris", "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf2064dceb9a92e6aa1952") }Following is the query to display all documents from a collection with the help of find() method −> db.findDataTypeDemo.findOne();This will produce the following output −{    "_id" : ObjectId("5ccf2064dceb9a92e6aa1952"),    "ClientName" : "Chris",    "isMarried" : false }Following is the query to find datatype of a field in MongoDB −> typeof db.findDataTypeDemo.findOne().isMarried;This will produce the following output −BooleanHere is the query to get the data type of another field −> ... Read More

How to do alphanumeric sort in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

437 Views

You need to set numericOrdering: true for alphanumeric sort. Let us first create a collection with documents −> db.alphanumericSortDemo.insertOne({"StudentId":"STU1010"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf149adceb9a92e6aa194c") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU1101"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf14a2dceb9a92e6aa194d") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU1901"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf14a9dceb9a92e6aa194e") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU908"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf14aedceb9a92e6aa194f") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU101"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf14b2dceb9a92e6aa1950") }Following is the query to display all documents from a collection with the help of find() method −> db.alphanumericSortDemo.find().pretty();This will produce the following output ... Read More

Reverse array field in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

197 Views

To reverse array field in MongoDB, you can use forEach(). Let us first create a collection with documents −> db.reverseArrayDemo.insertOne({"Skills":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccddf99dceb9a92e6aa1946") }Following is the query to display all documents from a collection with the help of find() method −> db.reverseArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ccddf99dceb9a92e6aa1946"),    "Skills" : [       "C",       "Java"    ] }Here is the query to reverse array field in MongoDB −> db.reverseArrayDemo.find().forEach(function (myDocument) { ...    var arrayValue = [ myDocument.Skills[1], myDocument.Skills[0] ]; ...    db.reverseArrayDemo.update(myDocument, { ... Read More

Advertisements