Found 1659 Articles for Big Data Analytics

How to push an array in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:38:25

411 Views

To push an array, use $push in MongoDB. Let us first create a collection with documents −> db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610339fac4d418a017856d") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610341fac4d418a017856e") } > db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610355fac4d418a017856f") } > db.demo399.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61035efac4d418a0178570") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610364fac4d418a0178571") }Display all documents from a collection with the help of find() method −> db.demo399.find();This will produce the following output −{ ... Read More

How to copy attributes in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:37:39

431 Views

To copy the value of one attribute to another, use $set along with update(). Let us create a collection with documents −> db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2019-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716dfcfb11e5c34d89915") } > db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2020-05-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716ebcfb11e5c34d89916") }Display all documents from a collection with the help of find() method −> db.demo55.find();This will produce the following output −{ "_id" : ObjectId("5e2716dfcfb11e5c34d89915"), "ShippingDate" : "", "date" : ISODate("2019-01-21T00:00:00Z") } { "_id" : ObjectId("5e2716ebcfb11e5c34d89916"), "ShippingDate" : "", "date" : ISODate("2020-05-12T00:00:00Z") }Following is the query to copy attributes in MongoDB −> db.demo55.find({}).forEach(function(c){ ... ... Read More

Set MongoDB $slice with a range?

AmitDiwan
Updated on 03-Apr-2020 12:35:28

192 Views

To set slice along with a range, use the $slice operator with parameters. These parameters are to be set for beginning position of the elements to be fetched and the 2nd parameter is for range. Let us create a collection with documents −> db.demo54.insertOne({"ListOfValues":[100, 2030, 5353, 7364, 635, 535, 524, 423, 2434, 1323, 799874, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27151ecfb11e5c34d89914") }Display all documents from a collection with the help of find() method −> db.demo54.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e27151ecfb11e5c34d89914"),    "ListOfValues" : [       100,       2030, ... Read More

MongoDB query to convert an array to a map of documents with n attributes?

AmitDiwan
Updated on 03-Apr-2020 12:36:17

689 Views

For this, you can use $map. Let us first create a collection with documents −> db.demo398.insertOne({ ...    "details":[ ...       { ...          "Name":"Chris", ...          "Age":22 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e8cedfac4d418a017856c") }Display all documents from a collection with the help of find() method −> db.demo398.find();This will produce the following output −{ "_id" : ObjectId("5e5e8cedfac4d418a017856c"), "details" : [ { "Name" : "Chris", "Age" : 22 } ] }Following is the query to convert an array ... Read More

How do I index “or” in MongoDB for indexing multiple fields?

AmitDiwan
Updated on 03-Apr-2020 12:33:44

219 Views

To index multiple fields, use ensureIndex() for a combination. With ensureIndex(), we can create an index and even pass multiple fields. Let us create a collection with documents −> db.demo53.ensureIndex({"StudentFirstName":1, "StudentAge":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo53.ensureIndex({"StudentFirstName":1, "StudentCountryName":1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 2,    "numIndexesAfter" : 3,    "ok" : 1 } >db.demo53.insertOne({"StudentFirstName":"Chris", "StudentAge":21, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e271431cfb11e5c34d89911") } >db.demo53.insertOne({"StudentFirstName":"David", "StudentAge":23, "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27143ccfb11e5c34d89912") } >db.demo53.insertOne({"StudentFirstName":"Mike", "StudentAge":24, "StudentCountryName":"AUS"}); { ... Read More

MongoDB query to convert from ObjectId to String

AmitDiwan
Updated on 03-Apr-2020 12:31:37

1K+ Views

To convert from ObjectId to String, use toString() in MongoDB. Let us create a collection with documents −> db.demo52.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27129bcfb11e5c34d89910") }Display all documents from a collection with the help of find() method −> db.demo52.find();This will produce the following output −{ "_id" : ObjectId("5e27129bcfb11e5c34d89910"), "StudentName" : "Chris" }Following is the query to convert ObjectId to String −> ObjectId("5e27129bcfb11e5c34d89910").toString(); ObjectId("5e27129bcfb11e5c34d89910")Now you can check whether the ObjectId is in string or not −> typeof ObjectId("5e27129bcfb11e5c34d89910").toString();This will produce the following output −StringRead More

Set a similar name from another column in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:30:22

110 Views

Simply loop with forEach() and set column value from another column. Let us create a collection with documents −> db.demo51.insert({"Name1":"Chris", "Name":"David", "Age":24}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Carol", "Name":"Mike", "Age":22}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Sam", "Name":"Bob", "Age":26}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo51.find();This will produce the following output −{ "_id" : ObjectId("5e27108ccfb11e5c34d8990d"), "Name1" : "Chris", "Name" : "David", "Age" : 24 } { "_id" : ObjectId("5e27108dcfb11e5c34d8990e"), "Name1" : "Carol", "Name" : "Mike", "Age" : 22 } { "_id" : ObjectId("5e27108ecfb11e5c34d8990f"), "Name1" : "Sam", "Name" : ... Read More

How do I get a value array (instead a json array) greater than 50 in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:29:00

79 Views

To avoid getting json array and get a value array, use $in. For greater than, use MongoDB $gt. Let us create a collection with documents −> db.demo50.save({"Value":40}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":20}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":510}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo50.find();This will produce the following output −{ "_id" : ObjectId("5e270c02cfb11e5c34d89903"), "Value" : 40 } { "_id" : ObjectId("5e270c05cfb11e5c34d89904"), "Value" : 100 } { "_id" : ObjectId("5e270c07cfb11e5c34d89905"), "Value" : 20 } { "_id" : ObjectId("5e270c11cfb11e5c34d89906"), "Value" ... Read More

Searching for an array entry via its id in a MongoDB collection and performing update

AmitDiwan
Updated on 03-Apr-2020 12:27:44

78 Views

To search for an array via id, use positional $ operator. For update, use the UPDATE in MongoDB. Let us create a collection with documents −> db.demo49.insertOne( ... { ... ...    "Name": "David", ...    "Details": [ ...       { ...          "_id": "D1234", ...          "Subject":"MySQL" ...       }, ...       { ...          "_id": "E234", ...          "Subject":"Java" ...       }, ...       { ...          "_id": "F456", ...       ... Read More

MongoDB query to get a specific number of items

AmitDiwan
Updated on 03-Apr-2020 12:25:41

217 Views

To get a specific number of items, use $slice operator in MongoDB. Let us create a collection with documents −> db.demo48.insertOne({"Name":["David", "Chris", "Sam", "Mike", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e270491cfb11e5c34d89901") }Display all documents from a collection with the help of find() method −> db.demo48.find();This will produce the following output −{ "_id" : ObjectId("5e270491cfb11e5c34d89901"), "Name" : [ "David", "Chris", "Sam", "Mike", "Carol" ] }Following is the query to get only 2 items −> db.demo48.find({}, {Name:{$slice: 2}});This will produce the following output −{ "_id" : ObjectId("5e270491cfb11e5c34d89901"), "Name" : [ "David", "Chris" ] }Read More

Advertisements