본문 바로가기

mongoDB

MongoDB query

db.inventory.insertMany([
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

db.inventory.find( {} )
SELECT * FROM inventory

동일한 값 검색

equal value query

db.inventory.find( { status: "D" } )
{ <field1>: { <operator1>: <value1> }, ... }
db.inventory.find( { status: { $in: ["A", "D" ] } } )

$or

db.inventory.find( { $or: [ { status: "A"}, { qty: { $lt:30 } } ] } )
This query example retreves all documents in the collection where the 'status' equals "A" or 'qty' is less than($lt) 30:

Query on embedded/nested documents

db.inventory.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
db.inventory.find( { size: { h:14, w: 21, uom: "cm" } } )
_This query should be same order with saved data order._ 
db.inventory.find( { "size.uom":"in" } )
db.inventory.find( { "size.h": { $lt: 15 } } )

Query an array for an element

To query if the array field contains at least one element with the specified value, use the filter { < field>: } where is teh element value.

The following example queries for all documents where tags in an array that contains the string "red" as one of its elements

db.inventory.find( { tags: "red" } )

To specify conditions on the elements in teh array field, use query operators in the query filter document:

{ <array field>: { <operator1>: <value1>, ... } }

// e.g.
db.inventory.find( { dim_cm: { $gt: 25 } } )
db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
db.inventory.find( { tags: ["red", "blank"] } )
db.inventory.find( { tags: { $all: ["red", "blank"] }})


{ <array field>: { <operator1>: <value1>, ... } }

$elemMatch !

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g. one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

You should check this query do not return the values which is gt 15 and lt 20 this return values that element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both

if you want query that gt 15 and lt 20 (15 < x < 20)
your query should look like this

db.inventory.find( {dim_cm: { $elemMatch: { $gt: 15, $lt: 20 } } } )
// Check the diffence of these two query
db.inventory.find( { dim_cm: {$gt: 15, $lt: 20 } } )

// $elemMatch is really important
db.inventory.find( { dim_cm: $elemMatch: { $gt: 15, $lt: 20 } } )

you can access to array index using dot notation.

(but should you quatation marks)
This code queries for all documents where the second element in the array dim_cm is greater than 25

db.inventory.find( {"dim_cm.1": { $gt: 25 } } )

$size : check size of array/ array length

db.inventory.find( { "tags": { $size: 3 } } )

projecting fields to return from query

const cursor = db.collection('inventory').find({
    status: 'A'
})

above query returns all fields

const cursor = db.collection('inventory').find({ status: 'A'}).project({ item: 1, status: 1});

but this query returns only item and status field because we put 'project'

excluding fields

const cursor = db.collection('inventory').find({ status: 'A' }).project({ status: 0, instock: 0});

including specific fields in embedded documents

const cursor = db.collection('inventory').find({ status: 'A'}).project({ item: 1, status: 1, 'size.uom': 1});

when the field's value is null

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])

querying { : null }

db.inventory.find( { item: null } )

//return
{ "_id": 1, "item": null}
{ "_id": 2 }

$type: 10

db.inventory.find( { item: { $type: 10 } } )

//return
{ "_id": 1, "item": null}
{ "_id": 2 }

the value of the item field is of BSON Type Null (type number 10)

$exists: false

db.inventory.find( { item: { $exists: false } } )

//return
{ "_id": 2 }

'mongoDB' 카테고리의 다른 글

Populate Models  (0) 2020.08.28