Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started with MongoDB #3

DZone's Guide to

Getting Started with MongoDB #3

Take a look at this tutorial to learn more about basic operations in MongoDB.

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

Hello everyone! In my previous article, I explained CRUD operations in MongoDB, which you can find here. In this article, I will explain some leftover parts like sorting, projection, comparison query operator, logical query operator, and many more.

Before starting, let's insert a document first:

>db.students.insertMany([ { "name":"Shubham", "age":24, "address":"Noida" }, { "name":"Rahul", "age":20, "address":"Noida" }, { "name":"Karan", "age":18, "address":"Noida" }, { "name":"Aman", "age":20, "address":"Noida" } ])

Comparison Query Operator:

$eq: Matches values that are equal to a specified value.

  • Syntax : { : { $eq: } }

Example: you can use the following query to retrieve all students who are the age of 24.

>db.students.find( { "age" : { $eq : 24 } });

$gt: Matches values that are greater than a specified value.

  • Syntax: { : { $gt: } }

$gte: $gte selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value.

  • Syntax: { : { $gte: } }

$lt: $lt selects the documents where the value of the field is less than (i.e. <) the specified value.

  • Syntax: { : { $lt: } }

$ne: Selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.

  • Syntax: { : { $ne: } }

$in: The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:

  • Syntax: { field: { $in: [, , ... ] } }

$nin: The $nin operator selects the documents where the value of a field does not equal any value in the specified array. To specify an $nin expression, use the following prototype:

  • Syntax: { field: { $nin: [, , ... ] } }
Example:
>db.students.find( { "age" : { $nin :[18,24] } });

Logical Query Operators:

$and: Returns all documents that match the conditions of both clauses.

  • Syntax: { $and: [ {}, {} , ... , {} ] }

Example:

>db.students.find( { $and : [

... {

... "name" : "Rahul"},{"age" : 20 }

... ]}

Result:

{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }

$not: Returns documents that do not match the query expression.

>db.students.find( { age: { $not: { $gt: 20 } } } )
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }

$nor: Returns all documents that fail to match both clauses.

>db.students.find( { $nor: [ { "name": "Shubham" }, { "age": 20 } ] } );

Result:
{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }

$or: - Returns all documents that match the conditions of either clause.

>db.students.find( { $or: [ { "name": "Shubham" }, { "age": 20 } ] } )

Result:
{ "_id" : ObjectId("5b002062cca46cacf6c74479"), "name" : "Shubham", "age" : 24, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }

$exists:
  • Syntax: { field: { $exists: } }

When true, $exists matches the documents that contain the field, including documents where the field value is null. If false, the query returns only the documents that do not contain the field.

Example:
>db.students.find({"address" : { $exists: true}});
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c74479"), "name" : "Shubham", "age" : 24, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }

Projection: Projection is used to filter your retrieved data. If we want to retrieve only specific files, not all.

An example would be if you want to retrieve all data from students who are the age of 20, but in the result, you don't want "_id" filed.

>db.students.find({age:20},{"_id" : 0})
Result:

{ "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "name" : "Aman", "age" : 20, "address" : "Noida" }

If you want to retrieve only the student name, then, for one field, you dont need to specify all fields value as zero. There is another way to retrieve, just specify the value of that field as one.

Example:
>db.students.find({age:20},{"name" : 1})
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman" }

cursor.limit(): Use the limit() method on a cursor to specify the maximum number of documents the cursor will return.

  • Syntax: db.collection.find().limit()

Example:

>db.students.find().limit(2)
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c74479"), "name" : "Shubham", "age" : 24, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }

Cursor.skip(): This method is basically used to skip your collected data.

>db.students.find().skip(2)
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }

To sort your data:

Ascending order: To sort your data in ascending order, use the following command:

db.students.find().sort({"age":1})
Result:

{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c74479"), "name" : "Shubham", "age" : 24, "address" : "Noida" }

Descending order: To sort your data in descending order, just use the above command with field value -1 instead of 1.

db.students.find().sort({"age":-1})

Result:-{ "_id" : ObjectId("5b002062cca46cacf6c74479"), "name" : "Shubham", "age" : 24, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447a"), "name" : "Rahul", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447c"), "name" : "Aman", "age" : 20, "address" : "Noida" }
{ "_id" : ObjectId("5b002062cca46cacf6c7447b"), "name" : "Karan", "age" : 18, "address" : "Noida" }

I hope this will help you to do some more basic operations in MongoDB.

This article was first published on the Knoldus blog.

Get comfortable using NoSQL in a free, self-directed learning course provided by RavenDB. Learn to create fully-functional real-world programs on NoSQL Databases. Register today.

Topics:
database ,mongodb ,getting started ,sorting

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}