Found 1659 Articles for Big Data Analytics

MongoDB to fetch documents with $or Operator

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

98 Views

The $or operator performs a logical OR operation on an array of two or more expressions. Let us create a collection with documents −> db.demo674.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }Display all documents from a collection with the help of find() method −> db.demo674.find();This will produce the following output −{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", ... Read More

What does the max field mean in the output of MongoDB db..stats( )?

AmitDiwan
Updated on 13-May-2020 10:00:27

58 Views

Use max field in order to limit the number of documents in the collection. Following is the query to use the max field in the capped collection −> db.createCollection("demo673", { capped : true, size : 100, max :50 } ) { "ok" : 1 }Let us create a collection with documents −> db.demo673.insertOne({Name:"John", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3ec7304263e90dac943e8") } > db.demo673.insertOne({Name:"Bob", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3ec7804263e90dac943e9") } > db.demo673.insertOne({Name:"David", Age:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3ec7f04263e90dac943ea") }Display all documents from a collection with the help of find() ... Read More

Fetch a specific document in MongoDB with array elements

AmitDiwan
Updated on 13-May-2020 09:58:05

191 Views

To fetch a specific document, use dot notation in MongoDB find(). Let us create a collection with documents −> db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Oppo"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Samsung"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"OnePlus"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }Display all documents from a collection with the help of find() method −> db.demo672.find();This will produce the following output −{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] } { "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { ... Read More

Get fields from multiple sub-documents that match a condition in MongoDB?

AmitDiwan
Updated on 13-May-2020 09:57:33

578 Views

To get fields from multiple sub-documents, use MongoDB aggregate with $unwind. Let us create a collection with documents −> db.demo671.insertOne( ... { ... ...    "details" : [ ...    { ...       "id" : "1" ...    }, ...    { ...       CountryName:"US", ...       "details1" : [ ...       { ...       "id" : "1" ...       }, ...       { ...          "id" : "2" ...       } ...       ] ... ... Read More

How do I access subdocuments in MongoDB queries?

AmitDiwan
Updated on 13-May-2020 09:53:25

725 Views

To access subdocuments in MongoDB, use find() with dot notation. Let us create a collection with documents −> db.demo670.insertOne({ ... id:101, ... "details": ... { ... Name:"Chris", ... Age:21, ... CountryName:"US", ... SubjectName:"MongoDB" ... } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3e31d04263e90dac943de") } > db.demo670.insertOne({ id:102, "details": { Name:"David", Age:22, CountryName:"UK", SubjectName:"MySQL" } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3e33604263e90dac943df") }Display all documents from a collection with the help of find() method −> db.demo670.find();This will produce the following output −{ "_id" : ObjectId("5ea3e31d04263e90dac943de"), "id" : 101, "details" : { "Name" ... Read More

Get the maximum element in MongoDB collection?

AmitDiwan
Updated on 13-May-2020 09:49:33

91 Views

To get the maximum element from the collection, sort in descending order with limit. Let us create a collection with documents −> > db.demo669.insertOne({"Marks":76}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3133c04263e90dac943d9") } > db.demo669.insertOne({"Marks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3133f04263e90dac943da") } > db.demo669.insertOne({"Marks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3134204263e90dac943db") } > db.demo669.insertOne({"Marks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3134504263e90dac943dc") } > db.demo669.insertOne({"Marks":79}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea3134e04263e90dac943dd") }Display all documents from a collection with the help of find() method −> db.demo669.find();This will produce the following output ... Read More

Implement array match in MongoDB?

AmitDiwan
Updated on 13-May-2020 09:47:46

130 Views

Use $all for array match. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo668.createIndex({"ListOfSubject":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo668.insert({"ListOfSubject":["MySQL", "Java", "C"]}); WriteResult({ "nInserted" : 1 }) > db.demo668.insert({"ListOfSubject":["MongoDB", "Python", "C++"]}); WriteResult({ "nInserted" : 1 }) > db.demo668.insert({"ListOfSubject":["C#", "Spring", "Hibernate", "MongoDB"]}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo668.find();This will produce the following output −{ ... Read More

Create ObjectId in MongoDB using a seed string?

AmitDiwan
Updated on 13-May-2020 09:47:13

290 Views

The ObjectId does not accept the seed string. You need to use _id − StringValue. Let us create a collection with documents −> db.demo667.insertOne({_id:"Chris"}); { "acknowledged" : true, "insertedId" : "Chris" } > db.demo667.insertOne({_id:"David"}); { "acknowledged" : true, "insertedId" : "David" } > db.demo667.insertOne({_id:"Chris"}); 2020-04-23T22:01:23.268+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo667 index: _id_ dup key: { : "Chris" } : WriteError({    "index" : 0,    "code" : 11000,    "errmsg" : "E11000 duplicate key error collection: test.demo667 index: _id_ dup key: {    : \"Chris\" }",    "op" : {       "_id" : ... Read More

Display only the employee names with specific salaries in MongoDB documents with employee records?

AmitDiwan
Updated on 13-May-2020 09:43:33

2K+ Views

To display only the employee names with specific salaries, set the salaries in MongoDB $in and fetch the names. Let us create a collection with documents> db.demo666.insertOne({"EmployeeName":"John", "EmployeeSalary":25000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c04824113ea5458c7d0d") } > db.demo666.insertOne({"EmployeeName":"Chris", "EmployeeSalary":35000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c05524113ea5458c7d0e") } > db.demo666.insertOne({"EmployeeName":"David", "EmployeeSalary":65000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c06024113ea5458c7d0f") } > db.demo666.insertOne({"EmployeeName":"Carol", "EmployeeSalary":40000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c06f24113ea5458c7d10") }Display all documents from a collection with the help of find() method −> db.demo666.find();This will produce the following output −{ "_id" : ObjectId("5ea1c04824113ea5458c7d0d"), "EmployeeName" ... Read More

Searching for ranges in MongoDB?

AmitDiwan
Updated on 13-May-2020 09:41:54

83 Views

To search for ranges, use sort() with limit(). Let us create a collection with documents −> db.demo665.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1424113ea5458c7d08") } > db.demo665.insertOne({"Value":15}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1b24113ea5458c7d09") } > db.demo665.insertOne({"Value":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1e24113ea5458c7d0a") } > db.demo665.insertOne({"Value":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf2324113ea5458c7d0b") } > db.demo665.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf2b24113ea5458c7d0c") }Display all documents from a collection with the help of find() method −> db.demo665.find();This will produce the following output −{ "_id" : ObjectId("5ea1bf1424113ea5458c7d08"), "Value" : 10 ... Read More

Advertisements