Found 1659 Articles for Big Data Analytics

MongoDB compound conditions to fetch documents?

AmitDiwan
Updated on 14-May-2020 10:10:06

83 Views

Let us create a collection with documents −> db.demo714.insertOne({FirstName:"Chris", LastName:"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam", LastName:"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David", LastName:"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John", LastName:"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }Display all documents from a collection with the help of find() method −> db.demo714.find();This will produce the following output −{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { ... Read More

Update tag records in MongoDB quickly

AmitDiwan
Updated on 14-May-2020 10:08:31

65 Views

Use $ along with update command to update tag records. Let us create a collection with documents −> db.demo713.insertOne( ...    { ...       tags: ...       [ ...          { ...             id:101, ...             Name:"Tag-1" ...          }, ...          { ...             id:102, ...             Name:"Tag-3" ...          }, ...          { ...           ... Read More

How to add a new field to all the documents in a MongoDB collection

AmitDiwan
Updated on 14-May-2020 10:06:37

407 Views

To add a new field, use $addFields in MongoDB. Let us create a collection with documents −> db.demo712.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f675d33e20ed1097b82") } > db.demo712.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6a5d33e20ed1097b83") } > db.demo712.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6e5d33e20ed1097b84") }Display all documents from a collection with the help of find() method −> db.demo712.find();This will produce the following output −{ "_id" : ObjectId("5ea85f675d33e20ed1097b82"), "Name" : "John" } { "_id" : ObjectId("5ea85f6a5d33e20ed1097b83"), "Name" : "Chris" } { "_id" : ObjectId("5ea85f6e5d33e20ed1097b84"), "Name" : "Bob" }Following is the query to add a ... Read More

MongoDB query to get documents with multiple conditions set in $or?

AmitDiwan
Updated on 14-May-2020 10:03:43

1K+ Views

Let us create a collection with documents −> db.demo711.insertOne({Name:"John", "Marks":75, Age:21, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c215d33e20ed1097b7e") } > db.demo711.insertOne({Name:"Chris", "Marks":55, Age:22, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c2c5d33e20ed1097b7f") } > db.demo711.insertOne({Name:"Bob", "Marks":45, Age:20, status:"Inactive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c3e5d33e20ed1097b80") } > db.demo711.insertOne({Name:"David", "Marks":85, Age:23, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c775d33e20ed1097b81") }Display all documents from a collection with the help of find() method −> db.demo711.find();This will produce the following output −{ "_id" : ObjectId("5ea85c215d33e20ed1097b7e"), "Name" : "John", "Marks" : 75, "Age" : 21, "status" : ... Read More

Sort id and reverse the items with MongoDB

AmitDiwan
Updated on 14-May-2020 10:01:05

906 Views

The $natural returns the documents in natural order. To reverse the items, use $natural:-1. Let us create a collection with documents −> db.demo710.insertOne({id:101, Name:"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a855d33e20ed1097b7a") } > db.demo710.insertOne({id:102, Name:"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a8d5d33e20ed1097b7b") } > db.demo710.insertOne({id:103, Name:"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a935d33e20ed1097b7c") } > db.demo710.insertOne({id:104, Name:"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a9b5d33e20ed1097b7d") }Display all documents from a collection with the help of find() method −> db.demo710.find();This will produce the following output −{ "_id" : ObjectId("5ea83a855d33e20ed1097b7a"), "id" : 101, "Name" : ... Read More

MongoDB aggregation to get two documents with the least marks

AmitDiwan
Updated on 14-May-2020 09:58:33

104 Views

To get the sorted list of marks, use $sort. Use $limit: 2 to display only two such documents with least marks. Let us create a collection with documents −> db.demo709.insertOne({Name:"John", "Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839005d33e20ed1097b76") } > db.demo709.insertOne({Name:"Chris", "Marks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839075d33e20ed1097b77") } > db.demo709.insertOne({Name:"David", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839125d33e20ed1097b78") } > db.demo709.insertOne({Name:"Bob", "Marks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839295d33e20ed1097b79") }Display all documents from a collection with the help of find() method −> db.demo709.find();This will produce the following output −{ "_id" ... Read More

Find MongoDB records based on a condition?

AmitDiwan
Updated on 14-May-2020 09:56:29

1K+ Views

To find MongoDB based on a condition, use find() and set the condition. Let us create a collection with documents −> db.demo708.insertOne({"Name":"John", Marks:54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702e4d346dcb074dc6f33") } > db.demo708.insertOne({"Name":"Chris", Marks:35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702e6d346dcb074dc6f34") } > db.demo708.insertOne({"Name":"David", Marks:45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702ebd346dcb074dc6f35") } > db.demo708.insertOne({"Name":"Bob", Marks:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702fad346dcb074dc6f36") }Display all documents from a collection with the help of find() method −> db.demo708.find();This will produce the following output −{ "_id" : ObjectId("5ea702e4d346dcb074dc6f33"), "Name" : "John", "Marks" : 54 ... Read More

Set server status to inactive in a MongoDB collection with server records?

AmitDiwan
Updated on 14-May-2020 09:55:33

289 Views

Let us create a collection with documents −> db.demo707.insertOne( ...    { ...       id:101, ...       "serverInformation": ...       [ ...          { ...             "IP":"192.56.34.3", ...             "Status":"Active" ...          }, ...          { ...             "IP":"192.56.36.4", ...             "Status":"Inactive" ...          } ...       ] ...    } ... ); {    "acknowledged" : true,     ... Read More

Display MongoDB with document and subdocument example and update

AmitDiwan
Updated on 14-May-2020 09:46:18

243 Views

Following is the syntax showing document and subdocument −db.yourCollectionName.insertOne(    {       yourFiledName:yourValue,       yourFieldName : [          {             yourFiledName1,             yourFiledName2,             .             .             .             N          }       ]    } );Let us see an example create a collection with documents −> db.demo706.insertOne( ...    { ...       PortalName: "GameApplication", ... Read More

MongoDB - Query embedded documents?

AmitDiwan
Updated on 14-May-2020 09:43:40

249 Views

To query embedded documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo705.insertOne( ...    { ...       _id:101, ...       "Information": ...       [ ...          { ...             "StudentName":"Chris", ...             "StudentAge":21 ...          }, ...          { ...             "StudentName":"David", ...             "StudentAge":23 ...          }, ...          { ...     ... Read More

Advertisements