Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by AmitDiwan
Page 542 of 839
Ignore first 4 values in MongoDB documents and display the next 3?
For this, use $slice and set thecount of values to be ignored and displayed. Let us create a collection with documents −> db.demo693.insertOne({Values:[10, 746, 736, 283, 7363, 424, 3535]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea58a04ece4e5779399c07b") } > db.demo693.insertOne({Values:[100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea58a1eece4e5779399c07c") }Display all documents from a collection with the help of find() method −> db.demo693.find();This will produce the following output −{ "_id" : ObjectId("5ea58a04ece4e5779399c07b"), "Values" : [ 10, 746, 736, 283, 7363, 424, 3535 ] } { "_id" : ObjectId("5ea58a1eece4e5779399c07c"), "Values" : ...
Read MoreMongoDB query to find documents with specific FirstName and LastName
To find documents with specific FirstName and LastName, use $and along with $in. Implement this in MongoDB find(). Let us create a collection with documents −> db.demo692.insertOne({FirstName:"Chris", "LastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585dca7e81adc6a0b396a") } > db.demo692.insertOne({FirstName:"John", "LastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585e2a7e81adc6a0b396b") } > db.demo692.insertOne({FirstName:"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585e7a7e81adc6a0b396c") } > db.demo692.insertOne({FirstName:"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585efa7e81adc6a0b396d") } > db.demo692.insertOne({FirstName:"Adam", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585faa7e81adc6a0b396e") }Display all documents from a collection with the help of find() ...
Read MoreSet condition in MongoDB nested document?
Let’s say we need to find a document with a value greater than specific value. For this, use dot notation in nested document and set the condition with $gt.Let us see an example and create a collection with documents −> db.demo688.insert( ... { ... information:{id:1, details:[ ... {otherDetails:{ ... values:75 ... } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo688.insert({ ... information: ... { ... id:2, ... details: ... [ ... {otherDetails:{ ... values:78 ... } ... } ...
Read MoreMongoDB aggregation to fetch documents with specific field value?
For this, use aggregate(). Let’s say we have to fetch documents with a field “Age” with value “21”.Let us now create a collection with documents −> db.demo685.insertOne( ... { ... "details": ... [ ... { ... Name:"Chris", ... Age:21 ... }, ... { ... Name:"David", ... Age:23 ... }, ... ...
Read MoreAccessing inner element of JSON array in MongoDB?
To access inner element of JSON array in MongoDB, use dot notation. Let us create a collection with documents −> db.demo687.insert({CountryName:'US', ... info: ... { ... id:101, ... details: ... [ ... { ... Name:'Chris', ... SubjectName:'MongoDB', ... otherDetails:{ ... "Marks":58, ... Age:23 ... } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo687.insert({CountryName:'UK', ... info: ... { ... id:102, ... details: ... [ ... { ... Name:'David', ... SubjectName:'MySQL', ... otherDetails:{ ... "Marks":78, ... ...
Read MoreHow to query MongoDB similar to “like” ?
To implement similar to “like”, use find() along with // in MongoDB. Let us create a collection with documents −> db.demo686.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea55182a7e81adc6a0b395c") } > db.demo686.insertOne({"FirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea55186a7e81adc6a0b395d") } > db.demo686.insertOne({"FirstName":"ROBERT"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea5518fa7e81adc6a0b395e") } > db.demo686.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea55195a7e81adc6a0b395f") } > db.demo686.insertOne({"FirstName":"robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea5519ba7e81adc6a0b3960") }Display all documents from a collection with the help of find() method −> db.demo686.find();This will produce the following output −{ "_id" : ...
Read MoreHow do I sort natural in MongoDB?
Use $natural to sort natural in MongoDB. Let us create a collection with documents −> db.demo684.insertOne({Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530cea7e81adc6a0b3957") } > db.demo684.insertOne({Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d1a7e81adc6a0b3958") } > db.demo684.insertOne({Value:60}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d4a7e81adc6a0b3959") } > db.demo684.insertOne({Value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d8a7e81adc6a0b395a") }Display all documents from a collection with the help of find() method −> db.demo684.find();This will produce the following output −{ "_id" : ObjectId("5ea530cea7e81adc6a0b3957"), "Value" : 10 } { "_id" : ObjectId("5ea530d1a7e81adc6a0b3958"), "Value" : 50 } { "_id" : ObjectId("5ea530d4a7e81adc6a0b3959"), ...
Read MoreWhat happens when we try to add a number to undefined value?
If you try to add a number to undefined value then you will get a NaN. The NaN defines Not a Number. Following is an example −Case 1var anyVar=10+undefined; print(anyVar) //Result will be NaNCase 2var anyVar1=10; var anyVar2; var anyVar=yourVar1+yourVar2; print(anyVar) //Result will be NaNCase 1Let us implement the above cases. The query is as follows −> var result=10+undefined; > print(result);This will produce the following output −NaNCase 2Let us implement the above case −> var value; > var value1=10; > var result=value1+value > resultThis will produce the following output −NaN
Read MoreCount the documents with a field value beginning with 13
To count the documents, use $count. For values beginning with 13, use $regex. You can use $regex. Let us create a collection with documents −> db.demo570.insertOne({Information:{Value:"13675"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e90959b39cfeaaf0b97b583") } > db.demo570.insertOne({Information:{Value:"14135"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095a739cfeaaf0b97b584") } > db.demo570.insertOne({Information:{Value:"15113"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095b639cfeaaf0b97b585") } > db.demo570.insertOne({Information:{Value:"13141"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095c139cfeaaf0b97b586") }Display all documents from a collection with the help of find() method −> db.demo570.find();This will produce the following output −{ "_id" : ObjectId("5e90959b39cfeaaf0b97b583"), "Information" : { "Value" : "13675" } } { "_id" : ObjectId("5e9095a739cfeaaf0b97b584"), "Information" : ...
Read MoreMongoDB query to match documents whose _id is in an array as part of a subdocument?
Let us create a collection with documents −> db.demo568.insertOne({ _id: 101, details: [ {id : 101 }, { id:103 } ] }); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo568.find();This will produce the following output −{ "_id" : 101, "details" : [ { "id" : 101 }, { "id" : 103 } ] } Following is the query to create second collection: > db.demo569.insertOne({ _id: 101, details: "John" }) { "acknowledged" : true, "insertedId" : 101 } > db.demo569.insertOne({ _id: 102, details: "Chris" }) { "acknowledged" : ...
Read More