db.collection.insertOne({}) Could include your own _id so that MongoDB does not create one for you. However, if you have two documents that are the same in every way except for _id, Mongo will interpret them as different documents
db.collection.insertMany([
{
// insertMany has ordered: true by default.
// This means that if there is an error inserting any item,
// the function ends and only the documents inserted before the error are inserted
},
// In many applications, we might want it to keep going after an error,
// in which case we would change ordered to false.
{
"ordered": false
}
])
You can create a document using update
_id: All collections have a unique primary index on _id field by default. MongoDB creates _id with ObjectID value
Each ObjectID is a 12-byte hex string
Date Mac PID Counter
objectID: _ _ _ _ | _ _ | _ _ | _ _ _
db.collection.find({ rated: 'PG-13'})
First argument to find is known as the query document
Fields in query document are selectors that restrict the result set
Selectors in the query document for find are implicitly &ed together
db.collection.find({ 'tomato.meter': 100 })
^ to access nested documents, use dot notation - has to be in quotes
db.collection.find({ "writers": ["Ethan Coen", "Joel Coen"]})
^ order matters: This will search the collection for documents where Ethan Cohen and Joel Coen appear in the "writers" field in that order and where they are the only two elements of that array.
db.collection.find({ "actors": "Jeff Bridges })
This will return any document where "Jeff Bridges" is an element of the array "actors"
db.collection.find({ "actors.0": "Jeff Bridges })
This will return documents where "Jeff Bridges" is the first name listed in the actors array
Find method returns a cursor To access documents, you need to iterate through a cursor In the mongo shell, if we don't assign a return value from find using the var keyword, the cursor automatically is iterated up to 20 times to print the initial search results
Handy way of reducing size of data returned for any one query
Projection syntax allows you to explicitly include fields in documents returned - can also explicitly exclude fields
db.collection.find({query document}, {projection document})
db.collection.find({ runtime: { $gt: 90 }})
// find documents where runtime is greater than 90
// field: { value the field should have }
.find({ runtime: { $gte: 90, $lte: 120 }})
// find movies with runtimes between 90 and 120
db.collection.find({ "tomato.meter": {
$gte: 95, runtime: { $gt: 180 }, {
title: 1, runtime: 1, _id: 0 }})
^ find movies with a tomato rating of >= 95 & runtime of > 180, then project title, runtime, and not _id
$eq has same semantics
db.collection.find({ rated: { $ne: "UNRATED" }})
// all documents that don't have unrated ratings
^ will also return documents without a rated field (if there are any) Rather than not having a 'null' in the field, mongoDB doesn't store that field at all
db.collection.find({ rated: { $in: ["G", PG"]}})
// all documents where rating is G or PG
value of $in has to be an array
$nin is for matching none of the values specified in an array
Considerations for the shape of a document: - Presence or absence/or data type of a field
db.collection.find({ "tomato.meter": { $exists: true }})
// returns documents that have a tomato.meter rating
db.collection.find({ "tomato.meter": { $exists: false }})
// returns documents that do not have a tomato.meter rating
db.collection.find({ "_id": { $type: "string" }})
//returns documents that have "_id" as a string rather than an ObjectId
db.collection.find({ $or : [ {
"tomato.meter": { $gt: 95 }}, {
"metacritic": $gt: 88 }]})
// takes array as argument, elements are criteria
db.collection.find({ $and : [ { "metacritic: { $ne: null }}, {
"metacritic": { $exists: true }}]})
// $and is necessary only in certain situations because of implicit &
// keys in JSON document have to be unique.
// $and allows you to place multiple constraints on the same field
db.collection.find({ "awards.text": { $regex: /^Won\s.*/ }})
^ means start at the beginning, Won means match with text that have Won as the beginning of the text, \s means space, .* means followed by any sequence of other characters
db.collection.find({ genres: { $all: ["Comedy", "Crime", "Drama" ]}})
// return documents that contain all three elements
db.collection.find({ countries: { $size: 1 }})
// documents where there was only one country listed
db.collection.find({ boxOffice: { $elemMatch: {
country: "UK", revenue: { $gt: 15 }}}})
// matches only when both country: UK and gt: 15 match in boxOffice
db.collection.updateOne({ filter/selector document }, {
$set { how we want to update the document}})
$set
takes a document as an argument and expects a document with a number of specified fields. Will update document matching the filter such that all key/value pairs are reflected in new version of document
db.collection.updateOne({ title: "The Martian" }, {
$inc: { "tomato.reviews": 3, "tomato.userReviews: 25 }})
// increments tomato reviews by 3 and userReviews by 25
db.collection.updateOne({ title: "The Martian" }, {
$push: { reviews: { $each [ { rating: 4.5, date: ..., etc.}]}})
db.collection.updateOne({ title: "The Martian"}, {$push: {
reviews: { $each: [ { keys/values }], $position: 0, $slice: 5}}})
// need to use $position to specify you want element at the front of the array
// This is saying to add this field as the first element of the reviews field array
// and that the array should only be 5 elements long
Same principles apply to updateMany but it will make the same modification to all documents that match the filter.
db.collection.updateMany({ rated: null }, { $unset: { rated: '' }})
// removes the field 'rated' from docs where rated is null
update any document where imdb.id is equal to imdb.id in detail document
db.collection.replaceOne({ "imdb": detail.imdb.id}, detail)
// wholesale document document replacement