Ignore First 4 Values in MongoDB Documents and Display Next 3

AmitDiwan
Updated on 14-May-2020 09:16:27

145 Views

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 More

MongoDB Query to Find Documents with Specific Firstname and Lastname

AmitDiwan
Updated on 14-May-2020 09:14:20

1K+ Views

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 More

Use of novalidate Attribute in HTML

Arushi
Updated on 14-May-2020 09:12:47

6K+ Views

The novalidate attribute in HTML is used to signify that the form won’t get validated on submit. It is a Boolean attribute and useful if you want the user to save the progress of form filing. If the form validation is disabled, the user can easily save the form and continue & submit the form later. While continuing, the user does not have to first validate all the entries.ExampleYou can try to run the following code to learn how to use novalidate attribute in HTML. In the following example, if you will add text in the  field, then it won’t show ... Read More

Rename a Collection in MongoDB

AmitDiwan
Updated on 14-May-2020 09:11:55

185 Views

To rename a collection in MongoDB, use renameCollection(). Let us create a collection with documents −> db.demo690.insertOne({_id:101, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo690.insertOne({_id:102, Name:"Mike"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo690.insertOne({_id:103, Name:"John"}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : 101, "Name" : "Sam" } { "_id" : 102, "Name" : "Mike" } { "_id" : 103, "Name" : "John" }Following is the query to rename collection −> db.demo690.renameCollection("demo691"); { "ok" ... Read More

MongoDB Insert Statement for Multiple Inserts

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

255 Views

For multiple insert, use insert() in MongoDB. Let us create a collection with document −> db.demo689.insert([ ...    {ClientName:"Chris", "ClientAge":34, "ClientCountryName":"US"}, ...    {ClientName:"David", "ClientAge":28, "ClientCountryName":"UK"}, ...    {ClientName:"Bob", "ClientAge":39, "ClientCountryName":"AUS"}, ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo689.find();This will produce the following output −{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3967"), "ClientName" : "Chris", "ClientAge" : 34, "ClientCountryName" : ... Read More

Set Condition in MongoDB Nested Document

AmitDiwan
Updated on 14-May-2020 09:04:56

344 Views

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 More

MongoDB Aggregation to Fetch Documents with Specific Field Value

AmitDiwan
Updated on 14-May-2020 09:03:57

683 Views

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 More

Access Inner Element of JSON Array in MongoDB

AmitDiwan
Updated on 14-May-2020 09:01:58

693 Views

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 More

Query MongoDB Similar to LIKE

AmitDiwan
Updated on 14-May-2020 08:59:59

379 Views

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 More

Sort Natural in MongoDB

AmitDiwan
Updated on 14-May-2020 08:52:35

880 Views

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 More

Advertisements