Found 1659 Articles for Big Data Analytics

How to return the position of a document relative to the collection in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:24:16

456 Views

To return the position of a document relative to the collection, use sort() along with count(). Let us create a collection with documents −> db.demo47.insertOne({"ClientName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267240cfb11e5c34d898f0") } > db.demo47.insertOne({"ClientName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267243cfb11e5c34d898f1") } > db.demo47.insertOne({"ClientName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267247cfb11e5c34d898f2") } > db.demo47.insertOne({"ClientName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e26724ccfb11e5c34d898f3") }Display all documents from a collection with the help of find() method −> db.demo47.find();This will produce the following output −{ "_id" : ObjectId("5e267240cfb11e5c34d898f0"), "ClientName" : "Adam" } { "_id" : ... Read More

Indexing large text field to make query faster in MongoDB

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

132 Views

To index large text field, use ensureIndex() along with $regex for text search. Let us create a collection with documents −> db.demo46.ensureIndex({"Name":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo46.insertOne({"Name":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267004cfb11e5c34d898ed") } > db.demo46.insertOne({"Name":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267009cfb11e5c34d898ee") } > db.demo46.insertOne({"Name":"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267011cfb11e5c34d898ef") }Display all documents from a collection with the help of find() method −> db.demo46.find();This will produce the following output −{ "_id" : ... Read More

How to get a saved object in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:21:09

211 Views

Let us first create a variable. Following is the query −> var studentDetails={"StudentFirstName":"Chris","StudentLastName":"Brown","StudentAge":24};Following is the query to save records using save() −> db.demo45.save(studentDetails); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> studentDetails;This will produce the following output −{    "StudentFirstName" : "Chris",    "StudentLastName" : "Brown",    "StudentAge" : 24,    "_id" : ObjectId("5e25dab4cfb11e5c34d898ec") }

How to store query output in temp MongoDB database?

AmitDiwan
Updated on 03-Apr-2020 12:19:23

580 Views

For this, in a single query, simply work with forEach() and store output in a temp db. Let us first create a collection with documents −> db.demo43.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25d4b3cfb11e5c34d898e5") } > db.demo43.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25d4b8cfb11e5c34d898e6") } > db.demo43.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25d4bbcfb11e5c34d898e7") }Display all documents from a collection with the help of find() method −> db.demo43.find();This will produce the following output −{ "_id" : ObjectId("5e25d4b3cfb11e5c34d898e5"), "StudentName" : "Chris" } { "_id" : ObjectId("5e25d4b8cfb11e5c34d898e6"), "StudentName" : "Bob" } { "_id" : ObjectId("5e25d4bbcfb11e5c34d898e7"), "StudentName" ... Read More

Rebuilding indexes in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:18:08

475 Views

To rebuild indexes, use reIndex(). Let us first create an index. Following is the query −> db.demo42.createIndex({"StudentFirstName":1});This will produce the following output −{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Following is the query to rebuild index in MongoDB −> db.demo42.reIndex({"StudentFirstName":1});This will produce the following output −{    "nIndexesWas" : 2,    "nIndexes" : 2,    "indexes" : [       {          "v" : 2,          "key" : {             "_id" : 1          },          "name" : "_id_",          "ns" : "web.demo42"       },       {          "v" : 2,          "key" : {             "StudentFirstName" : 1          },          "name" : "StudentFirstName_1",          "ns" : "web.demo42"       }   ],    "ok" : 1 }

MongoDB query to add multiple documents

AmitDiwan
Updated on 02-Apr-2020 14:31:11

161 Views

To perform multiple write operations, use bulkWrite(). Let us create an array list values. Following is the query −> const arrayList = [ ...    {"Value1":100, "Value2":200, "Name": "John"}, ...    {"Value1":100, "Value2":200, "Name": "Bob"} ... ]; > let op1 = []; > arrayList.forEach(({ Value1, Value2, Name }) => { ...    op1.push({ ...       "updateOne": { ...          "filter": { Name}, ...          "update": { "$set": { Value1, Value2, Name } }, ...          "upsert": true ...       } ...    }) ... }); > db.demo397.bulkWrite(op1); ... Read More

Perform $lookup to array of object id's in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 14:28:05

2K+ Views

For this, use $lookup. This performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing.Let us first create a collection with documents −> db.demo395.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e782317aa3ef9ab8ab207") } > db.demo395.insertOne({Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e782317aa3ef9ab8ab208") }Display all documents from a collection with the help of find() method −> db.demo395.find();This will produce the following output −{ "_id" : ObjectId("5e5e782317aa3ef9ab8ab207"), "Name" : "Chris" } { "_id" : ObjectId("5e5e782317aa3ef9ab8ab208"), "Name" : "David" }Let us create a second collection with documents ... Read More

How to query documents by a condition on the subdocument in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 14:24:10

160 Views

Let us first create a collection with documents −> db.demo394.insertOne( ...    { ... ...       details: [ ...       { ...          _id: '1', ...          startDate: '2018-01-11T07:00:00.000Z', ...          endDate: '2019-01-12T07:59:59.999Z' ...       }, ...       { ...          _id: '2', ...          startDate: '2019-01-21T07:00:00.000Z', ...          endDate: '2020-01-04T07:59:59.999Z' ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e716817aa3ef9ab8ab202") }Display all ... Read More

MongoDB query to collectively match intersection of documents against a field

AmitDiwan
Updated on 02-Apr-2020 14:21:18

296 Views

For this, use aggregate(). Let us first create a collection with documents −> db.demo393.insertOne( ...    { ...       Id1: "1", ...       Name: "Chris", ...       Id2: "100" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e6dd522064be7ab44e804") } > db.demo393.insertOne( ...    { ...       Id1: "1", ...       Name: "Chris", ...       Id2: "101" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e6dd522064be7ab44e805") } > db.demo393.insertOne( ...    { ...       Id1: "3", ... ... Read More

MongoDB query to pull multiple values from array

AmitDiwan
Updated on 02-Apr-2020 14:16:43

451 Views

To pull values, use $pull and set multi: true. Let us first create a collection with documents −> db.demo392.insertOne( ...    { ...       Name: 'Chris', ...       details: [ ...          { ...             _id: '101' ... ...          }, ...          { ...             _id: '102' ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d2b3322064be7ab44e802") } > > db.demo392.insertOne( ... Read More

Advertisements