0% found this document useful (0 votes)
3 views15 pages

Unit-II-NoSQL Example Programs

The document outlines experiments using MongoDB, focusing on query operators such as $exists, $type, $mod, and $regex. It includes instructions for setting up the MongoDB server and client, creating databases and collections, and executing various commands to manage data and users. Additionally, it provides examples of inserting data into collections and managing user roles and permissions.

Uploaded by

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

Unit-II-NoSQL Example Programs

The document outlines experiments using MongoDB, focusing on query operators such as $exists, $type, $mod, and $regex. It includes instructions for setting up the MongoDB server and client, creating databases and collections, and executing various commands to manage data and users. Additionally, it provides examples of inserting data into collections and managing user roles and permissions.

Uploaded by

chershicherry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No-5-Query Operators using MongoDB

Aim:
Practice exercise on element, array based and evaluation query operators -$exists, $type, $mod, $regex

Algorithm:
i)Start the server application MangoDB Community server.
ii)Start the Client Application MangoShell.
iii)Type and Execute the Database and Collection Creation command.
iv)Stop the Server and Client Applications.

Programs:

test> show dbs


acccorner 40.00 KiB
admin 180.00 KiB
config 60.00 KiB
cscorner 40.00 KiB
deltaxcorner 8.00 KiB
deltaxcorner1 72.00 KiB
deltaxcorner3 312.00 KiB
deltaxcorner44 40.00 KiB
deltaxcorner444 192.00 KiB
deltaxcorner5 120.00 KiB
ececorner 56.00 KiB
elango 160.00 KiB
itcorner 88.00 KiB
king 72.00 KiB
local 80.00 KiB
test> use deltaxcorner4

deltaxcorner4> db

deltaxcorner4> [Link]("Restaurant")

deltaxcorner4>
[Link]({"restaurant_id":1,"restaurant_name":"Taj","customer_name":"Ram","l
ocality":"Angallu","date":"11-Oct-2023","cuisine":"New","grade":"A","comments":"Good"})

[Link]({"restaurant_id":2,"restaurant_name":"Lemontree","customer_name":"
Ram","locality":"Angallu","date":"11-Oct-
2023","cuisine":"New","grade":"A","comments":"Good"})

deltaxcorner4> show collections

Restaurant

deltaxcorner4 > [Link]().pretty();

[
{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
restaurant_id: 1,
restaurant_name: 'Taj',
customer_name: 'Ram',
locality: 'Angallu',
date: '11-Oct-2023',
cuisine: 'New',
grade: 'A',
comments: 'Good'
}
]
deltaxcorner4 >[Link]()

[
{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
restaurant_id: 1,
restaurant_name: 'Taj',
customer_name: 'Ram',
locality: 'Angallu',
date: '11-Oct-2023',
cuisine: 'New',
grade: 'A',
comments: 'Good'
}
]
$exists

deltaxcorner3> [Link]('Restaurant').find({customer_name:{$exists:false}})
Nil

deltaxcorner3> [Link]('Restaurant').find({customer_name:{$exists:true}})
[
{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
restaurant_id: 1,
restaurant_name: 'Taj',
customer_name: 'Ram',
locality: 'Angallu',
date: '11-Oct-2023',
cuisine: 'New',
grade: 'A',
comments: 'Good'
}
]
$type:

deltaxcorner3> [Link]([{ $project: { restaurant_id : { $type:


"$restaurant_id" } }}])

[Link]([{ $project: { restaurant_id : { $type: "$restaurant_id" } }}])


[ { _id: ObjectId("6518dbd052c1c2bdb5c73032"), restaurant_id: 'int' } ]

deltaxcorner3> [Link]([{ $project: { customer_name : { $type:


"$customer_name" } }}])
[
{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
customer_name: 'string'
}
]

deltaxcorner3> [Link]([{ $project: { customer_job : { $type:


"$customer_job" } }}])

[
{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
customer_job: 'missing'
}
]
$regex
deltaxcorner3> [Link]({restaurant_name : {$regex: "Taj"}}).forEach(printjson)

{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
restaurant_id: 1,
restaurant_name: 'Taj',
customer_name: 'Ram',
locality: 'Angallu',
date: '11-Oct-2023',
cuisine: 'New',
grade: 'A',
comments: 'Good'
}

deltaxcorner3>[Link]({restaurant_name:{$regex: "Lemontree"}}).forEach(printjson)

{
_id: ObjectId("6518dbd052c1c2bdb5c73032"),
restaurant_id: 1,
restaurant_name: 'Taj',
customer_name: 'Ram',
locality: 'Angallu',
date: '11-Oct-2023',
cuisine: 'New',
grade: 'A',
comments: 'Good'
}
$mod

[Link]("inventory25")

[Link]( { qty: { $mod: [ 4, 0 ] } } )

Nil
[Link]( "inventory266")
[Link]( [
{ "_id" : 1, "item" : "abc123", "qty" : 5 },
{ "_id" : 2, "item" : "xyz123", "qty" : 10},
{ "_id" : 3, "item" : "ijk123", "qty" : 15 }
] )

{ ok: 1 }

[Link]( [
{ "_id" : 1, "item" : "abc123", "qty" : 4 },
{ "_id" : 2, "item" : "xyz123", "qty" : 8},
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }
] )

[Link]( { qty: { $mod: [ 4, 0 ] } } )

[
{ _id: 1, item: 'abc123', qty: 4 },
{ _id: 2, item: 'xyz123', qty: 8 },
{ _id: 3, item: 'ijk123', qty: 12 }
]
Experiment No-6- Exercise on MongoDB shell commands and user management

Aim:
To practice on MongoDB shell commands and user management using NoSQL.

Algorithm:
i)Start the server application MangoDB Community server.
ii)Start the Client Application MangoShell.
iii)Type and Execute the Database and Collection Creation command.
iv)Stop the Server and Client Applications.

Programs:
[Link];

[Link]

[Link]({user:"tom2",pwd:passwordPrompt(),roles:[]})

Enter password

bbb

***{ ok: 1 }

db. createUser({user:"tom3",pwd:passwordPrompt(), roles:[{role:"read",db:"acccorner"}]})

Enter password

ddd
***{ ok: 1 }

db. createUser({user:"tom13",pwd:passwordPrompt(), roles:[{role:"readWrite",db:"acccorner"}]})

Enter password

ccc

***{ ok: 1 }

[Link]("tom",passwordPrompt())

Enter password

ccc

***{ ok: 1 }

[Link];

Database Class:

getMongo Returns the current database connection

getName Returns the name of the DB

getCollectionNames Returns an array containing the names of all collections in

the current database.

getCollectionInfos Returns an array of documents with collection information,

i.e. collection name and options, for the current database.


runCommand Runs an arbitrary command on the database.

adminCommand Runs an arbitrary command against the admin database.

aggregate Runs a specified admin/diagnostic pipeline which does not

require an underlying collection.

getSiblingDB Returns another database without modifying the db variable

in the shell environment.

getCollection Returns a collection or a view object that is functionally

equivalent to using the db.<collectionName>.

dropDatabase Removes the current database, deleting the associated data

files.

createUser Creates a new user for the database on which the method is

run. [Link]() returns a duplicate user error if the user already exists on the database.

updateUser Updates the user’s profile on the database on which you run

the method. An update to a field completely replaces the previous field’s values. This includes

updates to the user’s roles array.

changeUserPassword Updates a user’s password. Run the method in the

database where the user is defined, i.e. the database you created the user.

logout Ends the current authentication session. This function has no

effect if the cu
[Link];

Collection Class:

aggregate Calculates aggregate values for the data in a collection or a

view.

bulkWrite Performs multiple write operations with controls for order of

execution.

count Returns the count of documents that would match a find() query

for the collection or view.

countDocuments Returns the count of documents that match the query for a

collection or view.

deleteMany Removes all documents that match the filter from a collection.

deleteOne Removes a single document from a collection.

distinct Finds the distinct values for a specified field across a single

collection or view and returns the results in an array.


estimatedDocumentCount Returns the count of all documents in a collection or

view.

find Selects documents in a collection or view.

findAndModify Modifies and returns a single document.

findOne Selects documents in a collection or view.

renameCollection Renames a collection.

show roles;

role: 'readWrite',

db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []

},

role: 'dbAdmin',
db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []

},

role: 'dbOwner',

db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []

},

role: 'userAdmin',

db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []
},

role: 'enableSharding',

db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []

},

role: 'read',

db: 'test',

isBuiltin: true,

roles: [],

inheritedRoles: []

[Link]("tom3")
{ ok: 1 }

You might also like