Skip to content

Instantly share code, notes, and snippets.

@robwilson1
Last active August 21, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilson1/fedf429396922db674fcabb161b2bd6d to your computer and use it in GitHub Desktop.
Save robwilson1/fedf429396922db674fcabb161b2bd6d to your computer and use it in GitHub Desktop.
MongoDB Essentials

Reading Documents

Basics

db.colleciton.find({}) - Find all docs in collection

db.collection.find({}).pretty() - Pretty print all docs in collection

db.collection.find({}).count() - Return the number of docs in collection

Finding scalar values

db.collection.find({ name: "john" }) - Finds all docs in collection where the name field equals "john"

db.collection.find({ name: "fred", age: 30 }) - Finds all docs in collection where the name field equals "fred" and the age equals 30.

db.collection.find({ name: "fred", age: 30 }).count() - Same as above but returns the count.

db.collection.find({ "address.postcode": "A1 234" }) - Finds all docs in collection where the postcode field inside the address object equals "A1 234". Notice the speech quotes wrapping the dot notation.

Finding array values

db.collection.find({ friends: [ "john", "fred" ] }) - Finds all docs in collection where the friends array matches the provided array exactly AND in the same order.

db.collection.find({ friends: "john" }) - Finds all docs in the collection where the friends array contains "john" at any index.

db.collection.find({ "friends.0": "john" }) - Finds all docs in the collection where the friends array contains "john" at the first index. Notice that we specify the index as if it were dot notation, and wrap it in speech quotes.

Equality operators

Link to all query operators

db.collection.find({ age: { $gt: 12, $lt: 20 } }) - Finds all docs in the collection where the age field is greater than 12 but less than 20. Notice that the equality operators can be comma delimited, and that each equality filter for a specific field is wrapped in curly braces.

db.collection.find({ age: { $gte: 18 }, income: { $lte: 30000 } }) - Finds all docs in the collection where the age field is greater than or equal to 18, and the income field is less than or equal to 30,000.

db.collection.find({ status: { $ne: "married" } }) - Finds all docs in the collection where the status field is anything other than "married". This will also return documents that do not have a status field at all.

db.collection.find({ name: { $in: ["john", "ben", "frank"] } }) - Finds all docs in the collection where the name field is "john", or "ben", or "frank". Note that the name field in this example is a scalar value (NOT an array), but using the $in operator, we must specify an array.

db.collection.find({ name: { $nin: ["john", "ben", "frank"] } }) - Finds all docs in the collection where the name field is NOT "john", or "ben", or "frank". Note that the name field in this example is a scalar value (NOT an array), but using the $nin operator, we must specify an array.

Element operators

db.collection.find({ address: { $exists: true } }) - Finds all documents that have an address field.

db.collection.find({ "_id": { $type: "string" } }) - Finds all documents where the _id field is of type string. (Not object ID)

Logical operators

$or

db.collection.find({
	$or: [
		{ name: "john" },
		{ age: { $gte 20 } }
	]
})

Finds all documents where the name field is equal to "john", and all documents where the age is greater then or equal to 20. Note the $or operator uses an array (square brackets).

$and

db.collection.find({
	$and: [
		{ name: "john" },
		{ age: { $gte 20 } }
	]
})

SAME AS:

db.collection.find({ name: "john", age: { $gte 20 } })

Finds all documents where both the name field is equal to "john" and the age field is greater than or equal to 20.

Notice that you dont have to specify the $and operator in this example as normal finds are implicetly and-ed. The reason to use $and is to perform matches on the same field. E.g:

db.collection.find({
	$and: [
		{ bestfriend: { $ne: null } },
		{ bestfriend: { $exists: true } }
	]
})

This will find all documents where the bestfriend field exists and is not a null value.

Array operators

db.collection.find({ friends: { $all: ["fred", "john", "jack"] } }) - Finds all documents where the friends array field contains "fred" and "john" and "jack"

db.collection.find({ friends: { $size: 5 } }) - Finds all documents where the friends array field has 5 elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment