Found 1661 Articles for Big Data Analytics

Return a specific field in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

375 Views

Too return a specific field, use the find() method in MongoDB. Let us first create a collection with documents −> db.specificFieldDemo.insertOne({"FirstName":"John", "LastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb8019a623186894665ae31") } > db.specificFieldDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb801ab623186894665ae32") } > db.specificFieldDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb801b3623186894665ae33") } > db.specificFieldDemo.insertOne({"FirstName":"Sam", "LastName":"Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb801bf623186894665ae34") }Following is the query to display all documents from the collection with the help of find() method −> db.specificFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb8019a623186894665ae31"),   ... Read More

How to drop a numeric collection from MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

166 Views

In order to remove the numeric collection name, use the following syntaxdb.getCollection("yourNumericCollectionName").drop();First, create a numeric collection. Following is the query> db.createCollection("2536464"); { "ok" : 1 }Now insert some documents in the above collection. Following is the query> db.getCollection("2536464").insertOne({"Record":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a46304881c5ce84b8e") } > db.getCollection("2536464").insertOne({"Record":2}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a76304881c5ce84b8f") } > db.getCollection("2536464").insertOne({"Record":3}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a96304881c5ce84b90") }Following is the query to display all documents from a collection with the help of find() method> db.getCollection("2536464").find().pretty();This will produce the following output{ "_id" : ObjectId("5ca254a46304881c5ce84b8e"), "Record" : 1 ... Read More

Get number of updated documents in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

275 Views

To get number of updated documents in MongoDB, you need to use runCommand along with getlasterror.Let us first create a collection with documents> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c1d6304881c5ce84bad") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c226304881c5ce84bae") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c276304881c5ce84baf") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Ramit"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c366304881c5ce84bb0") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c436304881c5ce84bb1") }Following is the query to display all documents from a collection with the help of find() method:> db.getNumberOfUpdatedDocumentsDemo.find().pretty();This will ... Read More

Finding highest value from sub-arrays in MongoDB documents?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

255 Views

To find the highest value from sub-arrays in documents, you can use an aggregate framework. Let us first create a collection with documents> db.findHighestValueDemo.insertOne(    ... {       ... _id: 10001,       ... "StudentDetails": [          ... { "StudentName": "Chris", "StudentMathScore": 56},          ... { "StudentName": "Robert", "StudentMathScore":47 },          ... { "StudentName": "John", "StudentMathScore": 98 }]    ... } ... ); { "acknowledged" : true, "insertedId" : 10001 } > db.findHighestValueDemo.insertOne(    ... {       ... _id: 10002,       ... "StudentDetails": [ ... Read More

MongoDB order by two fields sum?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

473 Views

To order by two fields sum, you can use the aggregate framework. Let us first create a collection with documents> db.orderByTwoFieldsDemo.insertOne({"Value1":10, "Value2":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca285576304881c5ce84baa") } > db.orderByTwoFieldsDemo.insertOne({"Value1":12, "Value2":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2855f6304881c5ce84bab") } > db.orderByTwoFieldsDemo.insertOne({"Value1":55, "Value2":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca285686304881c5ce84bac") }Following is the query to display all documents from a collection with the help of find() method> db.orderByTwoFieldsDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca285576304881c5ce84baa"),    "Value1" : 10,    "Value2" : 35 } {    "_id" : ObjectId("5ca2855f6304881c5ce84bab"),    "Value1" ... Read More

MongoDB equivalent of WHERE IN(1,2,…)?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

221 Views

The MongoDB equivalent of WHERE IN(1, 2, ....) is $in operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$in:[yourValue1, yourValue2, ....N]}}).pretty();Let us first create a collection with documents> db.whereInDemo.insertOne({"StudentName":"John", "StudentMathScore":57}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281ec6304881c5ce84ba5") } > db.whereInDemo.insertOne({"StudentName":"Larry", "StudentMathScore":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281f56304881c5ce84ba6") } > db.whereInDemo.insertOne({"StudentName":"Chris", "StudentMathScore":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281fd6304881c5ce84ba7") } > db.whereInDemo.insertOne({"StudentName":"Robert", "StudentMathScore":99}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2820a6304881c5ce84ba8") } > db.whereInDemo.insertOne({"StudentName":"Bob", "StudentMathScore":97}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca282206304881c5ce84ba9") }Following is the query to display all documents from a collection with ... Read More

How to project specific fields from a document inside an array in Mongodb?

George John
Updated on 30-Jul-2019 22:30:25

343 Views

To project specific fields from a document inside an array, you can use positional ($) operator.Let us first create a collection with documents> db.projectSpecificFieldDemo.insertOne(    ... {       ... "UniqueId": 101,       ... "StudentDetails" : [{"StudentName" : "Chris", "StudentCountryName ": "US"},          ... {"StudentName" : "Robert", "StudentCountryName" : "UK"},       ... ]       ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca27aeb6304881c5ce84ba2") } > db.projectSpecificFieldDemo.insertOne( { "UniqueId": 102, "StudentDetails" :    [{"StudentName" : "Robert", "StudentCountryName ": "UK"}, {"StudentName" : "David",    "StudentCountryName" : "AUS"}, ] ... Read More

How to query a key having space in its name with MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

2K+ Views

To query a key having space in its name, you can use dot(.) notation.Step 1: First, you need to create a set in which a key has space in its name. Following is the query:> myValues["Details"] = {} { } > myValues["Details"]["Student Name"]="John"; John > myValues["Details"]["StudentAge"]=26; 26Step 2: Now you need to create a collection and store the above set as a document. Following is the query> db.keyHavingSpaceDemo.insertOne( myValues); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca27e3b6304881c5ce84ba4") }Following is the query to display all documents from a collection with the help of find() method> db.keyHavingSpaceDemo.find().pretty();This will produce the following ... Read More

How to query an Array[String] for a Regular Expression match?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

195 Views

To query an array string for a regexp match, use the following syntaxdb.yourCollectionName.find( { yourFieldName: /yourStartingValue./ } ).pretty();Let us first create a collection with documents> db.queryArrayDemo.insertOne({"StudentFullName":["Carol Taylor", "Caroline Williams", "Claire Brown"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2774c6304881c5ce84ba0") } > db.queryArrayDemo.insertOne({"StudentFullName":["John Smith", "Jace Doe", "Jabin Brown"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca277b36304881c5ce84ba1") }Following is the query to display all documents from a collection with the help of find() method> db.queryArrayDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca2774c6304881c5ce84ba0"),    "StudentFullName" : [       "Carol Taylor",       "Caroline Williams",     ... Read More

How to find and modify a value in a nested array?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

221 Views

To find and modify a value in a nested array, you can use update command. Let us first create a collection with documents> db.findAndModifyAValueInNestedArrayDemo.insertOne( { "CompanyName" : "Amazon", "DeveloperDetails" : [ { "ProjectName" : "Online Book Store", "TeamSize" : "5" }, { "ProjectName" : "Library Management System", "TeamSize" : "7" }, { "ProjectName" : "Online Banking Application", "TeamSize" : "15" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca275226304881c5ce84b9f") }Following is the query to display all documents from a collection with the help of find() method> db.findAndModifyAValueInNestedArrayDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca275226304881c5ce84b9f"), ... Read More

Advertisements