Found 6705 Articles for Database

MongoDB query to update field and modify the data currently in column

AmitDiwan
Updated on 30-Mar-2020 12:08:25

445 Views

For this, use find() along with update(). Let us create a collection with documents −> db.demo115.insertOne({"LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efe9bd8f64a552dae635a") }Display all documents from a collection with the help of find() method −> db.demo115.find();This will produce the following output −{ "_id" : ObjectId("5e2efe9bd8f64a552dae635a"), "LastName" : "Brown" }Following is the query to update field and modify the data currently in column −> db.demo115.find({"LastName":"Brown"}).forEach(function(d) { ... db.demo115.update({_id: d._id}, {$set: {LastName: 'Hello ' + d.LastName}}); ... })Display all documents from a collection with the help of find() method −> db.demo115.find();This will produce the following output −{ "_id" : ... Read More

Find maximum score for duplicate Name values in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 12:06:54

166 Views

To find maximum score, use GROUP() to group documents in a collection. Let us create a collection with documents −> db.demo114.insertOne({"Score":60, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efc57d8f64a552dae6354") } > db.demo114.insertOne({"Score":87, "Nam+e":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efc5ad8f64a552dae6355") } > db.demo114.insertOne({"Score":45, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efc5dd8f64a552dae6356") } > db.demo114.insertOne({"Score":67, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efc68d8f64a552dae6357") } > db.demo114.insertOne({"Score":38, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2efc74d8f64a552dae6358") }Display all documents from a collection with the help of find() method −> db.demo114.find();This will produce the ... Read More

Creating views in MongoDB

AmitDiwan
Updated on 30-Mar-2020 12:03:56

1K+ Views

To create views in MongoDB, use createView(). Let us create a collection with documents −> db.demo113.insertOne( ... { _id: 1, StudentId: "101", "Details": { Name: "Chris", Age: 21 }, Subject: "MySQL" } ... ); { "acknowledged" : true, "insertedId" : 1 }Display all documents from a collection with the help of find() method −> db.demo113.find().pretty();This will produce the following output −{    "_id" : 1,    "StudentId" : "101",    "Details" : {       "Name" : "Chris",       "Age" : 21    },    "Subject" : "MySQL" }Following is the query to create views in MongoDB ... Read More

How to update a single field in a capped collection in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 12:01:40

301 Views

To update, you need to use update() for the field whose collection is set with capped: true. Let us create a collection with documents −> db.createCollection("Demo112", { capped : true, size : 14, max : 3 } ); { "ok" : 1 } > db.demo112.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ef47a9fd5fd66da21447e") } > db.demo112.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ef47e9fd5fd66da21447f") } > db.demo112.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ef4919fd5fd66da214480") }Display all documents from a collection with the help of find() method −> db.demo112.find();This will produce the following output −{ "_id" : ... Read More

MongoDB Aggregation to slice array inside array

AmitDiwan
Updated on 30-Mar-2020 11:58:52

435 Views

For this, use aggregate() in MongoDB. In that, use $slice to slice array inside array. Let us create a collection with documents −> db.demo111.insertOne( ...    { ...       "_id" : 101, ...       "Name" : "Chris", ...       "Details" : [ ...          { ...             "_id" : 101, ...             "Score" : 78, ...             "Subjects" : [ ...                { ...             ... Read More

Updating data in MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:53:51

194 Views

To update data in MongoDB, use update(). Let us create a collection with documents −> db.demo110.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2eeb949fd5fd66da21447b") } > db.demo110.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2eeb9f9fd5fd66da21447c") } > db.demo110.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2eeba39fd5fd66da21447d") }Display all documents from a collection with the help of find() method −> db.demo110.find();This will produce the following output −{ "_id" : ObjectId("5e2eeb949fd5fd66da21447b"), "Name" : "Chris" } { "_id" : ObjectId("5e2eeb9f9fd5fd66da21447c"), "Name" : "David" } { "_id" : ObjectId("5e2eeba39fd5fd66da21447d"), "Name" : "Bob" }Following is the query to update data in MongoDB ... Read More

How to only get the data of the nested JSON object in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 11:52:05

914 Views

To get the data of the nested JSON object in MongoDB, use findOne(). Let us create a collection with documents −> db.demo109.insertOne( ...    { ...       "Name" : "Chris", ...       "Subjects" : [ ...          { ...             "Id" : "100", ...             "Name":"MySQL", ...             "InstructorDetails" : [ ...                { ...                   "Name" : "John" ...         ... Read More

How to remove duplicate entries by two keys in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 11:47:54

778 Views

To remove duplicate entries by two keys, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo108.insertOne({"Value1":23, "Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3e49fd5fd66da214477") } > db.demo108.insertOne({"Value1":23, "Value2":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f29fd5fd66da214478") } > db.demo108.insertOne({"Value1":23, "Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f59fd5fd66da214479") }Display all documents from a collection with the help of find() method −> db.demo108.find();This will produce the following output −{ "_id" : ObjectId("5e2ee3e49fd5fd66da214477"), "Value1" : 23, "Value2" : 24 } { "_id" : ObjectId("5e2ee3f29fd5fd66da214478"), "Value1" : 23, "Value2" : 25 } { "_id" : ... Read More

How to work with variables in MongoDB query

AmitDiwan
Updated on 30-Mar-2020 11:45:53

3K+ Views

To use variables, work with var in MongoDB. Let us create a collection with documents −> db.demo107.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b19fd5fd66da214471") } > db.demo107.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b49fd5fd66da214472") } > db.demo107.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b89fd5fd66da214473") }Display all documents from a collection with the help of find() method −> db.demo107.find();This will produce the following output −{ "_id" : ObjectId("5e2ee1b19fd5fd66da214471"), "Name" : "Chris" } { "_id" : ObjectId("5e2ee1b49fd5fd66da214472"), "Name" : "Bob" } { "_id" : ObjectId("5e2ee1b89fd5fd66da214473"), "Name" : "David" }Following is the query to use variable in ... Read More

Insert array where element does not exist else update it (with multiple conditions)?

AmitDiwan
Updated on 30-Mar-2020 11:40:23

277 Views

You can use bulkWrite(). Let us create a collection with documents −> db.demo105.insertOne( { _id:'101', Name:'Chris', Details:[{ Marks1:60, Marks2:70, Marks3:70 }, { Marks1:70, Marks2:70, Marks3:90 }] } ); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() method −> db.demo105.find().pretty();This will produce the following output −{    "_id" : "101",    "Name" : "Chris",    "Details" : [       {          "Marks1" : 60,          "Marks2" : 70,          "Marks3" : 70       },       { ... Read More

Advertisements