Found 1349 Articles for MongoDB

Delete a field and value in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

386 Views

To delete a MongoDB field and value, you can use $unset operator. Let us first create a collection with documents −> db.deleteFieldDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb767219729fde21ddad") } > db.deleteFieldDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb837219729fde21ddae") } > db.deleteFieldDemo.insertOne({"FirstName":"Carol", "LastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9fb8d7219729fde21ddaf") }Following is the query to display all documents from the collection with the help of find() method −> db.deleteFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9fb767219729fde21ddad"),    "FirstName" : "John",    "LastName" : "Smith" } {    "_id" : ... Read More

How to read a specific key-value pair from a MongoDB collection?

Samual Sam
Updated on 30-Jul-2019 22:30:25

765 Views

You can use dot(.) notation to read a specific key-value pair from MongoDB collection. Let us first create a collection with documents −> db.readSpecificKeyValueDemo.insertOne( ...    { ...       "_id": 100, ...       "StudentDetails": ...       { ...          "StudentFirstName" : "David", ...          "StudentLastName" :"Miller", ...          "StudentAge":23, ...          "StudentCountryName":"US" ...       } ...    } ... ); { "acknowledged" : true, "insertedId" : 100 }Following is the query to display all documents from the collection with the ... Read More

How to remove duplicate values inside a list in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

896 Views

You can use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.removeDuplicatesDemo.insertOne({"InstructorName":"Chris", "InstructorAge":34, "InstructorSubject":    ["Java", "C", "Java", "C++", "MongoDB", "MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9d96c895c4fd159f80807") }Following is the query to display all documents from the collection with the help of find() method −> db.removeDuplicatesDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9d96c895c4fd159f80807"),    "InstructorName" : "Chris",    "InstructorAge" : 34,    "InstructorSubject" : [       "Java",       "C",       "Java",       "C++",       "MongoDB",   ... Read More

Fetch records in MongoDB on querying its subset

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

101 Views

You can use $all operator. Let us first create a collection with documents −> db.subsetOfAnArrayDemo.insertOne({"StudentProgrammingSkills":    ["Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9d1e1895c4fd159f80804") }Following is the query to display all documents from the collection with the help of find() method −> db.subsetOfAnArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9d1e1895c4fd159f80804"),    "StudentProgrammingSkills" : [       "Java",       "MongoDB",       "MySQL",       "C++",       "Data Structure",       "Algorithm",       "Python",     ... Read More

Extract subarray value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

359 Views

To extract the subarray value in MongoDB, you can use $elemMatch projection operator.Let us first create a collection with documents −> db.extractSubArrayDemo.insertOne( ...    { ...       _id: 101, ...       "clientName":"Larry", ...       "ClientDetails": ...       [ ...          { ...             "ClientProjectName":"Online Game", ...             "DeveloperTeamSize": 10 ...          }, ...          { ...             "ClientProjectName":"Pig Dice Game", ...             "DeveloperTeamSize": ... Read More

MongoDB query to get specific month|year (not date)?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

594 Views

You can use aggregate framework along with $month projection operator. Let us first create a collection with documents −> db.specificMonthDemo.insertOne({"StudentName":"Larry", "StudentDateOfBirth":new ISODate('1995-01-12')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9ca8f1d1b97daf71819") } > db.specificMonthDemo.insertOne({"StudentName":"Chris", "StudentDateOfBirth":new ISODate('1999-12-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9db8f1d1b97daf7181a") } > db.specificMonthDemo.insertOne({"StudentName":"David", "StudentDateOfBirth":new ISODate('2000-06-01')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9ee8f1d1b97daf7181b") }Following is the query to display all documents from the collection with the help of find() method −> db.specificMonthDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"),    "StudentName" : "Larry",    "StudentDateOfBirth" : ISODate("1995-01-12T00:00:00Z") } {    "_id" ... Read More

How do I add a value to the top of an array in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

153 Views

To add a value to the top of an array in MongoDB, you can use unshift() −yourArrayName.unshift("yourValue");The above syntax will add the value to the top of an array in MongoDB. Let us first create an array of strings −> technicalSkills=["Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL"];This will produce the following output −[ "Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL" ]Following is the query to add to the top of an array in MongoDB. Here, we will add “MongoDB” to top of an array using unshift() −> technicalSkills.unshift("MongoDB"); This will produce the following output −7Let us check the “MongoDB” has ... Read More

Finding matching records with LIKE in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

90 Views

Let us first create a collection with documents −> db.likeDemo.insertOne({"Name":"John", Age:32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb84984623186894665ae41") } > db.likeDemo.insertOne({"Name":"Chris", Age:25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb84991623186894665ae42") } > db.likeDemo.insertOne({"Name":"Carol", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849a1623186894665ae43") } > db.likeDemo.insertOne({"Name":"Johnny", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849b2623186894665ae44") } > db.likeDemo.insertOne({"Name":"James", Age:27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849bb623186894665ae45") }Following is the query to display all documents from the collection with the help of find() method −> db.likeDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

Converting isodate to numerical value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

347 Views

You can use getTime() for this. Following is the syntax −yourVariableName.getTime();Convert ISODate to numerical value −> var arrivalDate=ISODate('2019-04-18 13:50:45');Following is the query to convert ISODate to numerical value −> arrivalDate.getTime();This will produce the following output −1555595445000 Let us verify that it is a correct numerical value for ISODate or not. Following is the query to get correct ISODate whenever we apply above numeric value −> new Date(1555595445000);This will produce the following output −ISODate("2019-04-18T13:50:45Z")Yes, this is a correct ISODate.

MongoDB query to find property of first element of array?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

954 Views

You can use $slice operator for this. Let us first create a collection with documents −> db.firstElementOfArray.insertOne( ...    { ...       _id: 100, ...       "Details": [ ...          { ...             "CustomerName": "John", ...             "CustomerCountryName":"US" ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 100 } > db.firstElementOfArray.insertOne( ...    { ...       _id: 101, ...       "Details": [ ...       ... Read More

Advertisements