0% found this document useful (0 votes)
6 views2 pages

Mongodb

The document contains MongoDB commands for inserting, finding, updating, and deleting records in a 'BooksDB' collection. It includes examples of single and multiple inserts, various find queries with conditions, and methods for updating and deleting documents based on specific criteria. Additionally, it demonstrates how to check for duplicates and perform complex queries on an 'inventory' collection.

Uploaded by

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

Mongodb

The document contains MongoDB commands for inserting, finding, updating, and deleting records in a 'BooksDB' collection. It includes examples of single and multiple inserts, various find queries with conditions, and methods for updating and deleting documents based on specific criteria. Additionally, it demonstrates how to check for duplicates and perform complex queries on an 'inventory' collection.

Uploaded by

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

//insert one:

db.BooksDB.insertOne({
title: "Dead Silence",
author: "S.A. Barnes",
isbn: 1250819997,
price: 13.99,
available: true
})
//insertmany
db.BooksDB.insertMany([{
title: "Dead Silence",
author: "S.A. Barnes",
isbn: 1250819997,
price:erjs 13.99,
available: true},
{title: "Day Zero",
author: "C. Robert Cargill",
isbn: 0062405802,
price: 27.99,
available: true},
{title: "Sea of Rust",
author: "C. Robert Cargill",
isbn: 0062405803,
price: 21.99,
available: false}])

//find: select * from books;


db.BooksDB.find()

//select * from books where title="dayzero":


db.BooksDB.find({"title":"Day Zero"})

//select * from books where price > 14;


db.BooksDB.find({price : {$gt:14}})

// select title, isbn from books where price >14


db.BooksDB.find({price : {$gt:14}}, {title:1, isbn:1})
without id just type_id:0

//To retrieve only one record that satisfies specific search criteria, you can use
the findOne() method.
db.BooksDB.findOne({"title":"Dead Silence"})

// updateOne()

db.BooksDB.updateOne({author: "S.A. Barnes"}, {$set:{author: "Stacey Kade


Barnes"}})

// updateMany()
db.BooksDB.updateMany({author:"C. Robert Cargill"}, {$set: {author: "Christopher
Robert Cargill"}})

//deleteOne()
db.BooksDB.deleteOne({name:"Christopher Robert Cargill"})

//deleteMany()
db.BooksDB.deleteMany({author:"Christopher Robert Cargill"})
//checking duplicates
{
"name" : "ksqn291",
"__v" : 0,
"_id" : ObjectId("540f346c3e7fc1054ffa7086"),
"channel" : "Sales"
}
db.collection.aggregate([
{"$group" : { "_id": "$name", "count": { "$sum": 1 } } },
{"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } },
{"$project": {"name" : "$_id", "_id" : 0} }
]);

//SELECT * FROM inventory WHERE status in ("A", "D")


db.inventory.find({ status: { $in: [ "A", "D" ] } }).pretty()

//SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
db.inventory.find({ status: "A", $or: [ { qty: { $lt: 30 } }, { item:
/^p/ } ] }).pretty()

//TO REMOVE A DOCUMENT full


db.inventory.remove({})

//To remove a particulardocument


db.inventory.remove({name:"sdgf"})

You might also like