Skip to main content

NoSQL Document Database Queries

 

-          To check the current database list

cmd> show dbs;

-          To drop database

cmd> db.dropdatabase( );

-          To create collection

cmd> db.createCollection(“collection name”);

or

Use <collection name>

-          To insert a document into the collection

cmd> db.collectionname.insert([{}]); //insert many documents or one document

          db.collectionname.insertOne({}); //inserts one document

          db.collectionname.insertMany([{},{}]); //insert many documents

-          To Print data in Json format

cmd> db.collectionname.find( ).forEach(printjson);

-          Basic Find command return entire collection data

cmd> db.collectionname.find({} )

-       Search for a specific record and output should be in json format

cmd> db.collectionname.find({“fieldname”: value}).forEach(printjson)

 output will be:

{

“id”: ObjectId(“5b75cb657cb6057d416abef67”),

“employeeid”:1,

“emloyeename”: ”User”

}

-          Search for an employee whose value is greater than 2 and print out in json

Here we use query selection operator which is $gt and below is the query

and here is the link for query selection operators : https://docs.mongodb.com/manual/reference/operator/query/

cmd>db.collectionname.find({“employeeid”:{$gt:2}}).forEach(printjson)

-          If you want to limit the output results, then we need to use the following query

cmd> db.collectionname.find({}).limit(10)

-          Orders: One can specify the order of documents to be returned based on ascending or descending order of any key in the collection. Here is the command

cmd> db.collectionname.find({}).sort(“employeeid”:-1)

//-1 indicates that we want to return the documents based on the descending order of emplyeeid

-          To get the total count of the collection, we use this query

cmd> db.collectionname.count();

-          To delete a single document from the collection, then we need to run the following query

cmd> db.collectionname.remove({document ID});

-          To update a field or attribute in collection, then we need to run the following queries

Ø  Single Value

cmd> db.collectionname.update({“employeeId”:1},

{$set:{“employename”:”test”}});

Ø  Multiple Value

cmd> db.collectionname.update({“employeeId”:1},

{$set:{“ employename”:”test”, “employeeid”:22}});

NOTE: If you are using NoSQL booster, search for required document using find command, then you will see the output in Json format, now you go and change the it tree format, then right click on the any attribute you will see list of actions that you can perform, then click on “Edit Document” action, then it will opens in new tab and here you can update or add any attribute in that document and once done click F5 and it will updates the document successfully. --- In this process you can View Document, Edit Document, Add Document, Remove Document(s), Add Field, Remove Field/Value(S), Edit Field Value/Type and Rename Field etc. This reduces using of queries.

Screenshots will be provided soon…..

-          And, or condition to check multiple conditions at one run

cmd> db.collectionnames.find({$and:[

{$or:[{“employeeid”:{$eq:”789”}}]},

{$or:[{“employee.products.lob”:”games”}]}

]});

or

cmd> db.collectionnames.find({$and:[

{$or:[{“employeeid”:{$eq:”789”}}]},

{$or:[{“employee.products.lob”:”games”}]},

{$and:[{“employee.sales.lon”:”games”}]}

]});

-          Query by Data range, use starDate and endDate fields to set the range you want to look for and get results ordered by date in descending order

cmd> db.getCollection(“collection name”).aggregate(

[{

“$addFields”:{

“starDate”:”$;astUpdateDt”}},

{

“$addFields”:{

“endDate”:”$lastUpdateDt”}},

{

“$match”:{

“createdBy”:”xyz”,

“starDate”:”{$gte:ISODate(“2019-02-01T00:00.000-05:00”)},

“endDate”:”{$lte: ISODate(“2019-02-02T00:00.000-05:00”)},

}},

{

“$sort”:{“lastUpdateDt”:-1}}])

or

db.getCollection(“collection name”).aggregate(

[

{

“$addFields”: {

    “month(lastUpdateDt)”:{

“$month”: “$lastUpdateDt”

}}},

{

“$addFields”: {

    “year(lastUpdateDt)”:{

“$year”: “$lastUpdateDt”

}}},

{

“$match”:{

“createdBy”:”xyz”,

“month(lastUpdateDt)”: 3,

“year(lastUpdateDt)”: 2019

}},

{

“$limit: 100

}])

-          Regular Expression

cmd> db.collectionname.find({“_id”:{$regex:”PRE.*}}) // this will search for a document starting with PRE in it

db.collectionname.find({“_id”:{$regex:”.*PRE.*}}) //this will search for a document has PRE in the id

-          Query by any attribute and instead of returning all attributes in the collection, by this query you can only display the attributes that you want to

cmd> db.collectionname.find({“_id”:”12345”},{“actions.id”:1,”action.status”:1,”products.price.lob”:1});

-          Aggregate query

cmd> db.collectionname.aggregate([

{$match:{_id:”10023456”}},

{$unwind: “$policies”},

{$sort:{“policies.id”:1}},

{$group:{

_id:”$_id”,

policies:{$push:”policies”}

}}

Comments

Popular posts from this blog

SQL Real Time Interview Questions with solutions

SQL_Query : In this MySQL challenge, your query should return the names of the people who are reported to (excluding null values), the number of members that report to them, and the average age of those members as an integer. The rows should be ordered by the names in alphabetical order. Your output should look like the  following table . Solution : SELECT  ReportsTo ,   count ( ReportsTo )   as  Members ,   round ( avg ( Age ),   0 )   as   'Average Age'     FROM  maintable_MS9H0 Where  ReportsTo  is   not   null Group   by  ReportsTo order   by  ReportsTo or SELECT  ReportsTo , COUNT ( FirstName )   as  Members , ROUND ( AVG ( Age ))   as   "Average Age"   FROM  maintable_MS9H0  WHERE  ReportsTo  IS   NOT   NULL   GROUP   BY  ReportsTo SQL_Query: In this MySQL challenge, the table provided shows all new users signing up on a specific date in the format YYYY-MM-DD. Your query should output the change from one month to the next. Because the f

Defect Triaging and Priority vs Severity

 I fount the following sites explained clearly about defect triaging and felt worth to share it my blog to help others.  This question was been asked in many interviews as well "What is defect triaging? And how do you handle it?". Defect Triaging Meeting:  http://www.testingdiaries.com/defect-triage-meeting/#:~:text=Defect%20Triage%20Meetings%20are%20project,are%20defined%20for%20the%20bugs. Defect Triage:  https://www.guru99.com/bug-defect-triage.html#:~:text=Defect%20triage%20is%20a%20process,and%20priority%20of%20new%20defects. Priority Vs Severity:  https://www.guru99.com/defect-severity-in-software-testing.html https://www.softwaretestinghelp.com/how-to-set-defect-priority-and-severity-with-defect-triage-process/