0% found this document useful (0 votes)
75 views36 pages

Mongo Practice

This document shows the usage of MongoDB shell commands like db, show dbs, use, insertOne, find, dropDatabase, and getCollectionNames. It creates and populates sample collections to demonstrate CRUD operations on MongoDB documents.

Uploaded by

pvmharshavardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views36 pages

Mongo Practice

This document shows the usage of MongoDB shell commands like db, show dbs, use, insertOne, find, dropDatabase, and getCollectionNames. It creates and populates sample collections to demonstrate CRUD operations on MongoDB documents.

Uploaded by

pvmharshavardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

C:\Users\pvmhv>mongod --version

db version v7.0.8

Build Info: {

"version": "7.0.8",

"gitVersion": "c5d33e55ba38d98e2f48765ec4e55338d67a4a64",

"modules": [],

"allocator": "tcmalloc",

"environment": {

"distmod": "windows",

"distarch": "x86_64",

"target_arch": "x86_64"

C:\Users\pvmhv>mongosh

Current Mongosh Log ID: 6624f63b922b51f705117b7a

Connecting to:
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=
mongosh+2.2.4

Using MongoDB: 7.0.8

Using Mongosh: 2.2.4

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically
(https://www.mongodb.com/legal/privacy-policy).

You can opt-out by running the disableTelemetry() command.

------

The server generated these startup warnings when booting


2024-04-21T16:44:33.178+05:30: Access control is not enabled for the database. Read and write
access to data and configuration is unrestricted

------

test> show db

MongoshInvalidInputError: [COMMON-10001] 'db' is not a valid argument for "show".

test> show dbs

admin 40.00 KiB

config 12.00 KiB

local 40.00 KiB

test> use sampledb

switched to db sampledb

sampledb> db.createCollection("students")

{ ok: 1 }

sampledb> show dbs

admin 40.00 KiB

config 12.00 KiB

local 40.00 KiB

sampledb 8.00 KiB

sampledb> db sampledb

Uncaught:

SyntaxError: Missing semicolon. (1:2)

> 1 | db sampledb

| ^

2|

sampledb> show dbs local

admin 40.00 KiB

config 60.00 KiB

local 40.00 KiB


sampledb 8.00 KiB

sampledb> db sampledb

Uncaught:

SyntaxError: Missing semicolon. (1:2)

> 1 | db sampledb

| ^

2|

sampledb> show dbs

admin 40.00 KiB

config 60.00 KiB

local 40.00 KiB

sampledb 8.00 KiB

sampledb> db >db sampledb

Uncaught:

SyntaxError: Missing semicolon. (1:6)

> 1 | db >db sampledb

| ^

2|

sampledb> db:sampledb

ReferenceError: sampledb is not defined

sampledb> show dbs

admin 40.00 KiB

config 60.00 KiB

local 40.00 KiB

sampledb 8.00 KiB

sampledb> db db:sampledb

Uncaught:
SyntaxError: Missing semicolon. (1:2)

> 1 | db db:sampledb

| ^

2|

sampledb> db:sampledb

ReferenceError: sampledb is not defined

sampledb> use test

switched to db test

test> db sampledb

Uncaught:

SyntaxError: Missing semicolon. (1:2)

> 1 | db sampledb

| ^

2|

test> db:sampledb

ReferenceError: sampledb is not defined

test> db

test

test> use sampledb

switched to db sampledb

sampledb> db

sampledb

sampledb> show dbs

admin 40.00 KiB

config 60.00 KiB

local 40.00 KiB

sampledb 8.00 KiB


sampledb> db.movie.insert({"name":"tutorials point"})

DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.

acknowledged: true,

insertedIds: { '0': ObjectId('6624f7ad922b51f705117b7b') }

sampledb> db.movie.insertOne({"name":"tutorials point"})

acknowledged: true,

insertedId: ObjectId('6624f7ba922b51f705117b7c')

sampledb> show dbs

admin 40.00 KiB

config 108.00 KiB

local 40.00 KiB

sampledb 48.00 KiB

sampledb> show dbs sampledb

admin 40.00 KiB

config 108.00 KiB

local 40.00 KiB

sampledb 48.00 KiB

sampledb> show dbs local

admin 40.00 KiB

config 108.00 KiB

local 40.00 KiB

sampledb 48.00 KiB

sampledb> db.movie.find()

_id: ObjectId('6624f7ad922b51f705117b7b'),

name: 'tutorials point'


},

_id: ObjectId('6624f7ba922b51f705117b7c'),

name: 'tutorials point'

sampledb> db.getCollectionNames()

[ 'movie', 'students' ]

sampledb> db.dropDatabase

[Function: dropDatabase] AsyncFunction {

apiVersions: [ 1, Infinity ],

returnsPromise: true,

serverVersions: [ '0.0.0', '999.999.999' ],

topologies: [ 'ReplSet', 'Sharded', 'LoadBalanced', 'Standalone' ],

returnType: { type: 'unknown', attributes: {} },

deprecated: false,

platforms: [ 'Compass', 'Browser', 'CLI' ],

isDirectShellCommand: false,

acceptsRawInput: false,

shellCommandCompleter: undefined,

help: [Function (anonymous)] Help

sampledb> db.dropDatabase()

{ ok: 1, dropped: 'sampledb' }

sampledb> show dbs

admin 40.00 KiB

config 108.00 KiB

local 40.00 KiB

sampledb> use local

switched to db local

local> use test


switched to db test

test> db.getCollectionNames()

[]

test> use local

switched to db local

local> db.getCollectionNames()

[ 'startup_log' ]

local> use admin

switched to db admin

admin> db.getCollectionNames()

[ 'system.version' ]

admin> use config

switched to db config

config> use config

already on db config

config> db.getCollectionNames()

[ 'system.sessions' ]

config> use sample

switched to db sample

sample> db.getCollectionNames()

[]

sample> db.sampleCollection.insertOne({"name":"tutorials point"})

acknowledged: true,

insertedId: ObjectId('6624f959922b51f705117b7d')

sample> db.getCollectionNames()

[ 'sampleCollection' ]

sample> db.sampleCollections.find()

sample> db.sampleCollection.find()
[

_id: ObjectId('6624f959922b51f705117b7d'),

name: 'tutorials point'

sample> db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 100 }
)

MongoServerError[Location40415]: BSON field 'create.autoIndexID' is an unknown field.

sample> db.createCollection("mycol", { capped : true , max : 100 } )

MongoServerError[InvalidOptions]: the 'size' field is required when 'capped' is true

sample> db.createCollection("mycol", { capped : true , size : 614 } )

{ ok: 1 }

sample> db.mycol.insertOne("name":"Hi")

Uncaught:

SyntaxError: Unexpected token, expected "," (1:25)

> 1 | db.mycol.insertOne("name":"Hi")

| ^

2|

sample> db.mycol.insertOne({"name":"Hi"})

acknowledged: true,

insertedId: ObjectId('6624fc56922b51f705117b7e')

sample> db.mycol.find()

[ { _id: ObjectId('6624fc56922b51f705117b7e'), name: 'Hi' } ]

sample> db.mycol.insertOne({"name":"Hello"})

acknowledged: true,
insertedId: ObjectId('6624fc6f922b51f705117b7f')

sample> db.mycol.find()

{ _id: ObjectId('6624fc56922b51f705117b7e'), name: 'Hi' },

{ _id: ObjectId('6624fc6f922b51f705117b7f'), name: 'Hello' }

sample> db.mycol.insertOne({"name":"This"})

acknowledged: true,

insertedId: ObjectId('6624fc78922b51f705117b80')

sample> db.mycol.insertOne({"name":"is"})

acknowledged: true,

insertedId: ObjectId('6624fc7b922b51f705117b81')

sample> db.mycol.insertOne({"name":"harsha"})

acknowledged: true,

insertedId: ObjectId('6624fc7f922b51f705117b82')

sample> db.mycol.find()

{ _id: ObjectId('6624fc56922b51f705117b7e'), name: 'Hi' },

{ _id: ObjectId('6624fc6f922b51f705117b7f'), name: 'Hello' },

{ _id: ObjectId('6624fc78922b51f705117b80'), name: 'This' },

{ _id: ObjectId('6624fc7b922b51f705117b81'), name: 'is' },

{ _id: ObjectId('6624fc7f922b51f705117b82'), name: 'harsha' }

sample> db.mycol.storageSize()
36864

sample> db.mycol.find()

{ _id: ObjectId('6624fc56922b51f705117b7e'), name: 'Hi' },

{ _id: ObjectId('6624fc6f922b51f705117b7f'), name: 'Hello' },

{ _id: ObjectId('6624fc78922b51f705117b80'), name: 'This' },

{ _id: ObjectId('6624fc7b922b51f705117b81'), name: 'is' },

{ _id: ObjectId('6624fc7f922b51f705117b82'), name: 'harsha' }

sample> db.mycol.find().pretty()

{ _id: ObjectId('6624fc56922b51f705117b7e'), name: 'Hi' },

{ _id: ObjectId('6624fc6f922b51f705117b7f'), name: 'Hello' },

{ _id: ObjectId('6624fc78922b51f705117b80'), name: 'This' },

{ _id: ObjectId('6624fc7b922b51f705117b81'), name: 'is' },

{ _id: ObjectId('6624fc7f922b51f705117b82'), name: 'harsha' }

sample> db.mycol.find({"field": "value"})

sample> db.dropCollection()

TypeError: db.dropCollection is not a function

sample> db.mycol.drop()

true

sample> db.getCollectionNames()

[ 'sampleCollection' ]

sample> db.users.insertOne({ "name": "John", "age": 30, "email": "[email protected]" });

acknowledged: true,

insertedId: ObjectId('6624fdf9922b51f705117b83')

sample> db.users.find()
[

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 30,

email: '[email protected]'

sample> db.users.insertMany([

... { "name": "Alice", "age": 25, "email": "[email protected]" },

... { "name": "Bob", "age": 35, "email": "[email protected]" },

... { "name": "Eve", "age": 28, "email": "[email protected]" }

... ]);

acknowledged: true,

insertedIds: {

'0': ObjectId('6624fe14922b51f705117b84'),

'1': ObjectId('6624fe14922b51f705117b85'),

'2': ObjectId('6624fe14922b51f705117b86')

sample> db.users.find()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 30,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b84'),
name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.newusers.save({ "_id": ObjectId(), "name": "John", "age": 30, "email":


"[email protected]" });

TypeError: db.newusers.save is not a function

sample> var document = { "_id": ObjectId(), "name": "John", "age": 30, "email":
"[email protected]" };

sample> db.newusers.save(document);

TypeError: db.newusers.save is not a function

sample> var document = { "_id": ObjectId(), "name": "John", "age": 30, "email":
"[email protected]" };

sample> db.newusers.save(document);

TypeError: db.newusers.save is not a function

sample> var document = { "_id": ObjectId(), "name": "John", "age": 30, "email":
"[email protected]" };
sample> db.newusers.insert(document);

acknowledged: true,

insertedIds: { '0': ObjectId('6624fe87922b51f705117b8a') }

sample> var document = { "_id": 123, "name": "John", "age": 30, "email": "[email protected]" };

sample> db.newusers.insert(document);

{ acknowledged: true, insertedIds: { '0': 123 } }

sample> db.newusers.find()

_id: ObjectId('6624fe87922b51f705117b8a'),

name: 'John',

age: 30,

email: '[email protected]'

},

{ _id: 123, name: 'John', age: 30, email: '[email protected]' }

sample> db.newusers.find().toArray()

_id: ObjectId('6624fe87922b51f705117b8a'),

name: 'John',

age: 30,

email: '[email protected]'

},

{ _id: 123, name: 'John', age: 30, email: '[email protected]' }

sample> db.newusers.find()
[

_id: ObjectId('6624fe87922b51f705117b8a'),

name: 'John',

age: 30,

email: '[email protected]'

},

{ _id: 123, name: 'John', age: 30, email: '[email protected]' }

sample> db.newusers.find({ "age": { $gt: 25 } }).toArray()

_id: ObjectId('6624fe87922b51f705117b8a'),

name: 'John',

age: 30,

email: '[email protected]'

},

{ _id: 123, name: 'John', age: 30, email: '[email protected]' }

sample> db.users.find()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 30,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b84'),

name: 'Alice',

age: 25,
email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.find().toArray()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 30,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b84'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),
name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.find({ "age": { $gt: 25 } }).toArray()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 30,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

}
]

sample> db.users.update(

... { "name": "John" }, // Update documents where the name is "John"

... { $set: { "age": 31 } }, // Set the "age" field to 31

... { multi: true } // Update multiple documents that match the query

... )

DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.

acknowledged: true,

insertedId: null,

matchedCount: 1,

modifiedCount: 1,

upsertedCount: 0

sample> db.users.find({ "age": { $gt: 25 } }).toArray()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 31,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',
age: 28,

email: '[email protected]'

sample> var updatedDocument = { "_id": ObjectId("609aabc6b46cfa4eb1f67475"), "name": "John


Doe", "age": 32, "email": "[email protected]" };

sample> db.users.save(updatedDocument);

TypeError: db.users.save is not a function

sample> db.users.find({ "age": { $gt: 25 } }).toArray()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 31,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> var updatedDocument = { "_id": ObjectId(""), "name": "John Doe", "age": 32, "email":
"[email protected]" };
BSONError: input must be a 24 character hex string, 12 byte Uint8Array, or an integer

sample> var updatedDocument = { "_id": ObjectId(), "name": "John Doe", "age": 32, "email":
"[email protected]" };

sample> db.users.find({ "age": { $gt: 25 } }).toArray()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 31,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.find()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 31,
email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b84'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.insert(document);

{ acknowledged: true, insertedIds: { '0': 123 } }

sample> db.users.find()

_id: ObjectId('6624fdf9922b51f705117b83'),

name: 'John',

age: 31,

email: '[email protected]'

},
{

_id: ObjectId('6624fe14922b51f705117b84'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

{ _id: 123, name: 'John', age: 30, email: '[email protected]' }

sample> db.startup1.save({

... "_id" : "id6", "name":"MangoDB Document Replacing "

... })

TypeError: db.startup1.save is not a function

sample> db.startup1.save({ "_id": "123", "name": "MangoDB Document Replacing " })

TypeError: db.startup1.save is not a function

sample> db.users.save({ "_id": "123", "name": "MangoDB Document Replacing " })

TypeError: db.users.save is not a function

sample> var updatedDocument = { "_id": ObjectId("123"), "name": "MangoDB Document Replacing"


};

BSONError: input must be a 24 character hex string, 12 byte Uint8Array, or an integer


sample> var updatedDocument = { "_id": "123", "name": "MangoDB Document Replacing" };

sample> db.users.save(updatedDocument);

TypeError: db.users.save is not a function

sample> db.users.remove({ "name": "John" });

DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany,


findOneAndDelete, or bulkWrite.

{ acknowledged: true, deletedCount: 2 }

sample> db.users.find()

_id: ObjectId('6624fe14922b51f705117b84'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b85'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('6624fe14922b51f705117b86'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.remove({});

{ acknowledged: true, deletedCount: 3 }


sample> db.users.find()

sample> db.users.find()db.users.insertMany([

Uncaught:

SyntaxError: Missing semicolon. (1:15)

> 1 | db.users.find()db.users.insertMany([

| ^

2|

sample> { "name": "Alice", "age": 25, "email": "[email protected]" },

... { "name": "Bob", "age": 35, "email": "[email protected]" },

... { "name": "Eve", "age": 28, "email": "[email protected]" }

... ]);

Uncaught:

SyntaxError: Missing semicolon. (1:12)

>1| { "name": "Alice", "age": 25, "email": "[email protected]" },

| ^

2| { "name": "Bob", "age": 35, "email": "[email protected]" },

3| { "name": "Eve", "age": 28, "email": "[email protected]" }

4 | ]);

sample> db.users.insertMany([

... { "name": "Alice", "age": 25, "email": "[email protected]" },

... { "name": "Bob", "age": 35, "email": "[email protected]" },

... { "name": "Eve", "age": 28, "email": "[email protected]" }

... ]);

acknowledged: true,

insertedIds: {
'0': ObjectId('66250466922b51f705117b8c'),

'1': ObjectId('66250466922b51f705117b8d'),

'2': ObjectId('66250466922b51f705117b8e')

sample> db.users.insertMany([

... { "name": "Alice", "age": 25, "email": "[email protected]" },

... { "name": "Bob", "age": 35, "email": "[email protected]" },

... { "name": "Eve", "age": 28, "email": "[email protected]" }

... ]);

acknowledged: true,

insertedIds: {

'0': ObjectId('66250469922b51f705117b8f'),

'1': ObjectId('66250469922b51f705117b90'),

'2': ObjectId('66250469922b51f705117b91')

sample> db.users.find()

_id: ObjectId('66250466922b51f705117b8c'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250466922b51f705117b8d'),

name: 'Bob',

age: 35,

email: '[email protected]'
},

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b90'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.remove({ "name": "John" }, { justOne: true });

{ acknowledged: true, deletedCount: 0 }

sample> db.users.find()

{
_id: ObjectId('66250466922b51f705117b8c'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250466922b51f705117b8d'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b90'),

name: 'Bob',

age: 35,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),
name: 'Eve',

age: 28,

email: '[email protected]'

sample> db.users.insertMany([

... { "name": "Alice", "age": 25, "email": "[email protected]" },

... { "name": "Bob", "age": 35, "email": "[email protected]" },

... { "name": "Eve", "age": 28, "email": "[email protected]" }

... ]);

acknowledged: true,

insertedIds: {

'0': ObjectId('662504de922b51f705117b92'),

'1': ObjectId('662504de922b51f705117b93'),

'2': ObjectId('662504de922b51f705117b94')

sample> db.users.remove({ "name": "bob" }, { justOne: true });

{ acknowledged: true, deletedCount: 0 }

sample> db.users.remove({ "name": "Bob" });

{ acknowledged: true, deletedCount: 3 }

sample> db.users.remove({ "name": "Alice" }, { justOne: true });

{ acknowledged: true, deletedCount: 1 }

sample> db.users.find()

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'
},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b94'),

name: 'Eve',

age: 28,

email: '[email protected]'

sample> // Query to include only the "name" and "email" fields from the documents

sample> db.users.find({}, { "name": 1, "email": 1 })

{
_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b94'),

name: 'Eve',

email: '[email protected]'

sample> db.users.find({}, { "name": 1, "email": 1 ,"_id":0})

{ name: 'Eve', email: '[email protected]' },

{ name: 'Alice', email: '[email protected]' },

{ name: 'Eve', email: '[email protected]' },

{ name: 'Alice', email: '[email protected]' },


{ name: 'Eve', email: '[email protected]' }

sample> db.users.find()

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b94'),

name: 'Eve',
age: 28,

email: '[email protected]'

sample> db.users.find().limit(4)

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

age: 25,

email: '[email protected]'

]
sample> // Skip the first 5 documents and return the rest from the "users" collection

sample> db.users.find().skip(5)

sample> db.users.find().skip(1)

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b94'),

name: 'Eve',

age: 28,

email: '[email protected]'

]
sample> // Sort the documents in ascending order based on the "age" field from the "users"
collection

sample> db.users.find().sort({ age: 1 })

_id: ObjectId('66250469922b51f705117b8f'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b92'),

name: 'Alice',

age: 25,

email: '[email protected]'

},

_id: ObjectId('66250466922b51f705117b8e'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('66250469922b51f705117b91'),

name: 'Eve',

age: 28,

email: '[email protected]'

},

_id: ObjectId('662504de922b51f705117b94'),
name: 'Eve',

age: 28,

email: '[email protected]'

sample> // Creating an index on the "name" field in ascending order in the "users" collection

sample> db.users.createIndex({ name: 1 })

name_1

sample> // Finding indexes in the "users" collection

sample> db.users.getIndexes()

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { name: 1 }, name: 'name_1' }

sample> // Dropping the index named "name_1" from the "users" collection

sample> db.users.dropIndex("name_1")

{ nIndexesWas: 2, ok: 1 }

sample> // Dropping all indexes in the "users" collection

sample> db.users.dropIndexes()

nIndexesWas: 1,

msg: 'non-_id indexes dropped for collection',

ok: 1

sample> db.users.getIndexes()

[ { v: 2, key: { _id: 1 }, name: '_id_' } ]

sample> db.users.createIndex({ name: 1 })


name_1

sample> db.users.createIndex({ nam: 1 })

nam_1

sample> db.users.getIndexes()

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { name: 1 }, name: 'name_1' },

{ v: 2, key: { nam: 1 }, name: 'nam_1' }

sample> db.users.dropIndex("name_1")

{ nIndexesWas: 3, ok: 1 }

sample> db.users.getIndexes()

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { nam: 1 }, name: 'nam_1' }

sample> db.users.createIndex({ name: 1 })

name_1

sample> db.users.createIndex({ names: 1 })

names_1

sample> db.users.getIndexes()

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { nam: 1 }, name: 'nam_1' },

{ v: 2, key: { name: 1 }, name: 'name_1' },

{ v: 2, key: { names: 1 }, name: 'names_1' }

sample> db.users.getIndexes()

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { nam: 1 }, name: 'nam_1' },


{ v: 2, key: { name: 1 }, name: 'name_1' },

{ v: 2, key: { names: 1 }, name: 'names_1' }

sample> db.users.dropIndexes()

nIndexesWas: 4,

msg: 'non-_id indexes dropped for collection',

ok: 1

sample> db.users.getIndexes()

[ { v: 2, key: { _id: 1 }, name: '_id_' } ]

sample>

You might also like