Using Mongosh :
use natours-test
show dbs
// db is the currently used database, tours is the collection in which we want to insert the documents in.
// we can actually simply pass a JavaScript object in the inserMany(...), it'll then convert it into JSON and BSON
// insertMany(...) expects an array of documents
// insertOne(...) expects a single document
db.tours.insertOne({name: "The Forest Hiker", price: 297, rating: 4.7 })
db.tours.insertMany([{ name: "The Sea Explorer", price: 497, rating: 4.8 }, { name: "The Snow Adventurer", price: 997, rating: 4.9, difficulty: "easy" }])
// to find the storage path :
db.adminCommand("getCmdLineOpts").parsed.storage.dbPath
tours
which have a price less below 500
:
$lte
means <=
: name: 1
means that we only want name
to appear. Similarly, setting a field to 0
means that we do not that field to appear.updateOne
and updateMany
, now how does updateMany
work ? First we need to select the objects that we need to update then we have to pass in the data that should be updated.updateOne
will only update the first matched document.Adding a new field using update query :
premium
has now been added to the last documentreplaceOne
function. It works the same way as updateOne
does, but it replaces the entire document instead.Model.prototype
in JavaScript refers to an object created of a class.