-
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
Post a Comment