Filtering Flashcards

1
Q

Mongo DB:

Select records from collection where field1 equals value

A

db.collection.find({field1: value})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

MongoDB

Select records from collection where field1 greater than value

A

db.collection.find({field1: {$gt: value}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

MongoDB

Select records from collection where field1 lower than value

A

db.collection.find({field1: {$lt: value}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Mongo DB

Select records from collection where field1 lower than or equal to value

A

db.collection.find({field1: {$lte: value}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Mongo DB

Select records from collection where field1 greater than or equal to value

A

db.collection.find({field1: {$gte: value}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Mongo DB

Select records from collection using and condition

A

db.collection.find($and: [{condition1}, {condition2}])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mongo DB

Select records from collection using regex

A

db.collection.find({field: {$regex: “pattern”}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Mongo DB

Select records from collection which have a given size

A

db.collection.find({field: {$size: value}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Mongo DB

Select records from collection which have a given field

A

db.collection.find({field: {$exists: true}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Mongo DB

Select records from collection which don’t have a given field

A

db.collection.find({field: {$exists: false}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Mongo DB

Select records from collection sorted by a given field ascending

A

db.collection.find().sort({field: 1})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mongo DB

Select records from collection sorted by a given field descending

A

db.collection.find().sort({field: -1})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Mongo DB

Select records from collection limited by a given amount

A

db.collection.find().limit(limitCount)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Mongo DB

Select records from collection skiped by a given amount

A

db.collection.find().skip(skipCount)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Mongo DB

Select records from collection including specific fields only

A

db.collection.find({}, {field1: 1, fields2: 1})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Mongo DB

Select records from collection excluding specific fields

A

db.collection.find({}, {field1: 0, fields2: 0})

17
Q

Mongo DB

How to filter records using nested values?

A

db.collection.find(“address.city”, “Mexico DF”)

18
Q

Mongo DB

How to filter using conditions?

A

db.collection.find(subject: “math”, score: {$gt: 90})

19
Q

Mongo DB

How to filter certain results of a given array from a specific document?

A

db.collection.find({_id: ObjectId(“your_document_id”), arrayfield: {$elementMatch: {field1: “value1”, field2: {$gte: 10}}}})

20
Q

Mongo DB

How to filter certain results of a given array from a all documents?

A

db.collection.aggregate([{$match: {arrayField: {$elementMatch: {field1: “value1”, field2: {$gte: 20}}}}}])

21
Q

Mongo DB

How to filter records from and array inside another array?

A

db.collection.find({“outerArray”: {$elementMatch: {“innerArray”: {$elemMatch: {field1: “value”, field2: {$gte: 10}}, field: “value”}}}})