MongoDB Query examples

Here we will go through the different mongoDB query.The mains query that are used are  insert,remove ,update and Projection. There are examples provided for each of the mongoDB query.

The insert () Method

To insert information into MongoDB accumulation, you have to utilize MongoDB’s insert () or save () method.

Sentence structure

Fundamental language structure of insert () command is as mentioned below:

>db.collection_name.insert(document)

 

Example :

>db.mycol.insert({
_id: Objectid(7df78ad8902c),
title: 'MongoDB Overview',
depiction: 'MongoDB is no sql database',
by: 'j2eebrain',
url: 'http://www.j2eebrain.com',
labels: ['MongoDB', 'database', 'Nosql'],
likes: 100
})

Here mycol is the collection name, as made in past exercise. On the off chance that the collection doesn’t exist in the database, then MongoDB will make this collection and afterward insert record into it.

In the inserted document on the off chance that we don’t indicate the _id parameter, then MongoDB doles out a remarkable Objectid for this document.

_id is 16 bytes hexadecimal number novel for each document in an accumulation. 12 bytes are separated as takes after:

_id: Objectid(8 bytes timestamp, 4 bytes machine id,
4 bytes process id, 6 bytes incrementer)

To insert different documents in single query , you can execute the command as shown below.

Example

>db.post.insert([

{
title: 'MongoDB Introduction',
portrayal: 'MongoDB is no sql database',
by: ' j2eebrain ',
url: 'http://www.j2eebrain.com',
labels: ['MongoDB', 'database', 'Nosql'],
likes: 100
},

{
title: 'Nosql Database',
depiction: 'Nosql database doesn't have tables',
by: 'j2eebrain',
url: 'http://www.j2eebrain.com',
labels: ['MongoDB', 'database', 'Nosql'],
likes: 20,

remarks: [
{
user:'user1',
message: 'My first education',
datecreated: new Date(2014,12,11,3,33),
like: 0
}
]
}
])

To insert the document you can utilize db.post.save(document) additionally. On the off chance that you don’t determine _id in the document then save() technique will work in the same way as insert () strategy. On the off chance that you detail _id then it will supplant entire information of document containing _id as tagged in save() method .

The remove () Method

MongoDB’s remove () method is utilized to remove file from the collection. remove () method acknowledges two parameters to execute.

Deletion area:  deletion criteria as indicated by files will be removed.

Just one: (Optional) if set to genuine or 2 then remove stand out file.

Syntax structure: Essential syntax structure of remove () method is as mentioned below.

>db.collection_name.remove(deletion_area)

Example

Consider the mycol collection has after information.

{ "_id" : Objectid(87354297456839754rf45ec5), "title":"mongoDB Overview"}
{ "_id" : Objectid(87355489826f45ec6), "title":"My SQL Overview"}
{ "_id" : Objectid(8745637235487736534773df45ec7), "title":"J2eebrain Overview"}

Emulating illustration will remove all the documents whose title is ‘Mongodb Overview’

>db.mycol.remove({'title':'mongodb Overview'})
>db.mycol.find()
{ "_id" : Objectid(5983548781331adf45ec6), "title":"no sql Overview"}
{ "_id" : Objectid(5983548781331adf45ec7), "title":" J2eebrain intro"}
>

Remove method –In the event that there are different records and you need to erase just first record, then set justone parameter in remove() method:

>db.collection_name.remove(deletion_criteria,1)

Remove all documents

In the event that you don’t tag deletion criteria, then mongodb will erase entire documents from the collection. This is the likeness of SQL’s truncate order.

>db.mycol.remove()
>db.mycol.find()
>

MongoDB’s redesign () and spare () methods are utilized to overhaul document into an accumulation. The upgrade () method redesign values in the current document while the spare () method replaces the current document with the document passed in spare() method.

MongoDB Update () method

The update () method updated values in the current document.

Linguistic structure:

Essential sentence structure of update () method is as below

db.collection_name.update(selectioin_criteria,Updated_data)

MongoDB Projection – 

In mongodb projection significance is selecting just important information instead of selecting entire of the information of a document. In the event that a document has 5 fields and you have to show just 3, then select just 3 fields from them.

Projection in Mongodb refers to the  showing all the select data rather than the whole data. In Mongodb when you execute find() method, then it shows all fields of a document. To point of confinement this you have to set rundown of fields with values as 1 or 0.

1 is utilized to demonstrate the field while 0 is utilized to shroud the field.

Syntax:

Essential syntac of the find () method with projection is as below

>db.collection_name.find({},{key:1})