db.getCollectionInfos()
db.getCollectionNames()
db.collection.find()
db.inventory.find(
// filter condition
{
size: {
h: 14,
w: 21,
uom: 'cm'
// won't match any docs
// uom: 'cm'
// w: 21,
// h: 14,
},
'some field that need to be existed': {
$exists: true
}
'some text field': {
$regex: /^bc/,
},
// or 'some text field': /^bc/
},
// projection paramter
// use projection operator here
{
size: 1,
'some field that you want to show': 1
'_id': 0
// usually _id since it always show defaultly, the only way to not show it is set it to 0, redundant if used with fields other than _id
}
)
Note: Field Order Matters
Using 'dot notation'
Note: in 'dot notation', the FIELD and NESTED FIELD must be inside quotation marks
db.inventory.find({
'size.uom': 'inch'
})
// using comparison operator
db.inventory.find({
// implicit AND between 'size.h' and 'size.w'
'size.h': {
// implicit AND between $gt and $lt
$gt: 25,
$lt: 30,
},
'size.w': {
$gt: 30
}
// $eq - equilvalent to direct way. why people still use it? idk
// $lte
// $gte
// $in
// $ne
// $nin
})
// using logical operator
// remember, it wrapped OUTSIDE the field values
use('sample_restaurants')
db.restaurants
.find({
$and: [
{
borough: {
// implicit AND between $exists and $eq
$exists: true,
$eq: 'Brooklyn' // now i understand why $eq operator here
}
},
{
$or: [
{
cuisine: 'Italian',
},
{
cuisine: 'Caribbean',
},
],
},
],
})
// match 'fully', order matter
find({tags: ["red", "blank"]})
// match 'partially', no order
find({
tags: {
$all: ['red', 'blank']
}
})
// no condition
find({
tags: "red" // NOTE: no square brackets [] here
})
// all element meets multiple criteria
find({
dim_cm: {
$gt: 15,
$lt: 20
}
})
// at least 1 element meets multiple criteria
find({
dim_cm: {
$elemMatch: {
$gt: 15,
$lt: 20
}
}
})
// array index position matter
// array use zero-based indexing
find({
// 2nd element
'dim_cm.1': {
$gt: 25
}
})
// array length
find({
dim_cm: {
$size: 3
}
})