Found 1659 Articles for Big Data Analytics

How I can use a database-name with special characters like " customer_tracker-990" in MongoDB console

AmitDiwan
Updated on 13-May-2020 10:25:43

339 Views

Yes, use getSiblingDB(). Let us add some documents to the database −> use customer_tracker-990; switched to db customer_tracker-990 > db.demo1.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4697ca7e81adc6a0b3954") } > db.demo1.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46980a7e81adc6a0b3955") } > db.demo1.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46984a7e81adc6a0b3956") }Display all documents from a collection with the help of find() method −> db.getSiblingDB("customer_tracker-990").demo1.find();This will produce the following output −{ "_id" : ObjectId("5ea4697ca7e81adc6a0b3954"), "Name" : "Chris" } { "_id" : ObjectId("5ea46980a7e81adc6a0b3955"), "Name" : "David" } { "_id" : ObjectId("5ea46984a7e81adc6a0b3956"), "Name" : "Bob" }Read More

Limit the number of documents in a collection in MongoDB?

AmitDiwan
Updated on 13-May-2020 10:24:09

372 Views

To limit the number of documents in a collection, set capped − true. Set the size there itself. Let us create a collection with documents −> db.createCollection( "demo683", { capped: true, size: 5 ,max:4} ) { "ok" : 1 } > db.demo683.insertOne({Value:100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468afa7e81adc6a0b394e") } > db.demo683.insertOne({Value:500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b0a7e81adc6a0b394f") } > db.demo683.insertOne({Value:1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b1a7e81adc6a0b3950") } > db.demo683.insertOne({Value:400}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b2a7e81adc6a0b3951") } > db.demo683.insertOne({Value:800}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b3a7e81adc6a0b3952") } ... Read More

Is there any way to skip some documents in MongoDB?

AmitDiwan
Updated on 13-May-2020 10:22:17

102 Views

Yes, you can skip some documents using skip() in MongoDB. Use limit() to display how many documents you want to display after skipping some. Let us create a collection with documents −> db.demo682.insertOne({FirstName:"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); {    "acknowledged" : true,    "insertedId" ... Read More

Get the top most document from a MongoDB collection

AmitDiwan
Updated on 13-May-2020 10:19:35

131 Views

To get the topmost document, use find() along with limit(). To fetch only a single document, consider using limit(1). Let us create a collection with documents −> db.demo681.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo681.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo681.insertOne({_id:103, Name:"David"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo681.insertOne({_id:104, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo681.insertOne({_id:105, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo681.find();This will produce the following output −{ ... Read More

Set $gt condition in MongoDB $and

AmitDiwan
Updated on 13-May-2020 10:18:33

176 Views

The $and performs a logical AND operation on an array of one or more expressions. Let us create a collection with documents −> db.demo680.insertOne({Values:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461b04263e90dac943fe") } > db.demo680.insertOne({Values:70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461e04263e90dac943ff") } > db.demo680.insertOne({Values:[80, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4462a04263e90dac94400") } > db.demo680.insertOne({Values:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4463304263e90dac94401") }Display all documents from a collection with the help of find() method −> db.demo680.find();This will produce the following output −{ "_id" : ObjectId("5ea4461b04263e90dac943fe"), "Values" : 40 } { "_id" : ... Read More

Pull an element in sub of sub-array in MongoDB?

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

344 Views

To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −> db.demo679.insertOne( ...    { ...       id:1, ...       "details": [ ...          { ...             CountryName:"US", ...             "information": [ ... ...                { "Name": "Chris", "FirstName": "Name=Chris" }, ... ...                {"Name": "Bob", "FirstName": "Name=Bob" } ...             ] ...          }, ... ... Read More

How to index my collection to use a compound multikey index?

AmitDiwan
Updated on 14-May-2020 09:24:10

53 Views

For this, use ensureIndex(). Let us create a collection with documents −> db.demo678.ensureIndex({id:1,"details.userId":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo678.insertOne( ...    { ...       id:101, ... ...       "details" : [ ...          { ...             "userId" : "1001", ...             "userName":"Chris" ...          }, ...          { ...             "userId" : "1002", ...             "userName":"David" ...          } ...       ], ...       "otherDetails" : [ ...          { ...             CountryName:"US", ...             EmailId:["Chris@gmail.com","David@gmail.com"] ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4276904263e90dac943fc") }Display all documents from a collection with the help of find() method −> db.demo678.find();This will produce the following output −{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [    { "userId" : "1001", "userName" : "Chris" },    { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [    { "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] } ] }

MongoDB query to add up the values of a specific field in documents

AmitDiwan
Updated on 13-May-2020 10:09:06

225 Views

Let us create a collection with documents −> db.demo677.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421f404263e90dac943f8") } > db.demo677.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421f704263e90dac943f9") } > db.demo677.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421fa04263e90dac943fa") } > db.demo677.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea421fe04263e90dac943fb") }Display all documents from a collection with the help of find() method −> db.demo677.find();This will produce the following output −{ "_id" : ObjectId("5ea421f404263e90dac943f8"), "Value" : 10 } { "_id" : ObjectId("5ea421f704263e90dac943f9"), "Value" : 50 } { "_id" : ObjectId("5ea421fa04263e90dac943fa"), "Value" : 20 } { "_id" : ... Read More

MongoDB Group query to get the count of repeated marks in documents?

AmitDiwan
Updated on 13-May-2020 10:06:36

157 Views

For group query, use MongoDB $group and get the count with $sum. Let us create a collection with documents −> db.demo676.insertOne({"Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea41eed04263e90dac943f2") } > db.demo676.insertOne({"Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea41ef304263e90dac943f3") } > db.demo676.insertOne({"Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea41ef404263e90dac943f4") } > db.demo676.insertOne({"Marks":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea41ef704263e90dac943f5") } > db.demo676.insertOne({"Marks":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea41ef804263e90dac943f6") }Display all documents from a collection with the help of find() method −> db.demo676.find();This will produce the following output −{ "_id" : ... Read More

Set regex in MongoDB $in?

AmitDiwan
Updated on 13-May-2020 10:03:57

100 Views

Let us create a collection with documents −> db.demo675.insertOne({ ... "ListOfNames":["John", "Chris", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f5b404263e90dac943ef") } > db.demo675.insertOne({ "ListOfNames":["Bob", "Johnson", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f5c804263e90dac943f0") } > db.demo675.insertOne({ "ListOfNames":["Jace", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f5d304263e90dac943f1") }Display all documents from a collection with the help of find() method −> db.demo675.find();This will produce the following output −{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : ... Read More

Advertisements