arozwalak
1/23/2016 - 9:01 PM

MongoDB: CRUD

MongoDB: CRUD

//Create documents
// creates a collection if it doesn't exist

db.moviesScratch.insertOne({ "title": "Rocky", "year": 1976, "imdb": "tt0075148" });

{
	"_id": ObjectId("5693f0d861914c7244186a34"),
	"title": "Rocky",
	"year": 1976,
	"imdb": "tt0075148"
}

// setting _id explicitly

db.moviesScratch.insertOne({ "title": "Rocky", "year": 1976, "_id": "tt0075148" });

{
	"_id": "tt0075148",
	"title": "Rocky",
	"year": 1976
}

// insert many records at once. By default insert ordered, this will stop if error occurse (ie. duplicated _id)

db.moviesScratch.insertMany(
	[
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" }
	]
);

// insert many with `unordered` option. If it get error will try to add the rest of documents

db.moviesScratch.insertMany(
	[
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" },
		{ "title": "Rocky", "year": 1976, "imdb": "tt0075148" }
	],
	{ "ordered": false }
);

Read documents

db.movieDetails.find({ rated: "PG-13" }).pretty();

// look for documents that are rated "PG-13" AND from 2009 
db.movieDetails.find({ rated: "PG-13", year: 2009 }).pretty();

db.movieDetails.find({"tomato.meter": 100}).pretty();

{
	"_id": "tt0075148",
	"title": "Rocky",
	"year": 1976,
	"tomato": {
		"meter": 100,
		"date": "10-01-2010"
	}
}

// match Arrays

// match array exactly how it is, including order
db.movieDetails.find({"writers": ["Ethan Coen", "Joel Coen"] }).count()

// match documents which contains value in array
db.movieDetails.find({"actors": "Jeff Bridges"}).count()

// match documents which contains value on specific place in array
db.movieDetails.find({"actors.0": "Jeff Bridges"}).count()


// Projections
db.movieDetails.find({"actors": "Jeff Bridges"}, {"title": 1, "actors": 1, "_id": 0}).pretty()

// query operators [$eq, $gt, $gte, $lt, $lte, $ne, $in, $nin]
db.movieDetails.find({ runtime: { $gt: 90 }).pretty()

//runtime range 90 - 120
db.movieDetails.find({ runtime: { $gt: 90, $lt: 120 }).pretty()

db.movieDetails.find(
	{
		"tomato.meter": { $gte: 95}, 
		"runtime": {$gt: 180}
	}
).pretty();

// $ne operator will also return documents which don't have field
db.movieDetails.find({ rated: { $ne: "UNRATED"}).pretty();

// $in value must be array
db.movieDetails.find({ rated: { $in: ["G", "PG", "PG-13"] }}).pretty();

// elements operators
db.movieDetails.find({"tomato.meter": { $exists: true}}).pretty()

db.movieDetails.find({ "_id": {$type: "string"}}).count()


// logic operators
db.movieDetails.find({ $or: [ {"tomato.meter": { $gt:  95}}, {"metacritic":  {$gt: 88}} ]}).pretty()

db.movieDetails.find({ $and: [ {"metacritic": { $ne:  null}}, {"metacritic":  {$exists: true}} ]}).pretty()


// regular expression operator
db.movieDetails.find({ "awards.text": { $regex: /^Won\s.*/}}).pretty()

// array operators

// gets documents which genres field contains all of elements from $all array explicitly
db.movieDetails.find({ genres: $all: ["Comedy", "Crime", "Drama"]}).pretty()

// match documents based on length of array
db.movieDetails.find({ countries: { $size: 1}}).pretty()


boxOffice: [
	{"country": "USA", "revenue": 41.3 },
	{"country": "Australia", "revenue": 2.9 },
	{"country": "UK", "revenue": 10.1 },
	{"country": "Germany", "revenue": 4.3 },
	{"country": "France", "revenue": 3.5 },
]

db.movieDetails.find({ boxOffice: {$elemMatch: { "country": "UK", "revenue": { $gt: 15} } } }).pretty()


Update documents

// update one document
db.movieDetails.updateOne({"title": "The Martian"}, { $set: { poster: "link_to_poster.jpg"}});

db.movieDetails.updateOne({title: "The Martian"}, {$inc: {"tomato.reviews": 3, "tomato.userReviews": 25}});


// update array
$addToSet - adds an element to array only if they do not already exist in the set
$pop - removes first or last item of an array
$pullAll - removes all matching values from an array
$pull - removes all array elements that match a specified query
$pushAll - [Deprecated] adds several items to an array
$push - adds an item to an array