DEMO: findOne() Method
Insert data set :
Insert Embedded 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" }
]);
• Match an Embedded/nested documents as below –
To specify equality condition, provide exact value of the nested field
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
Examples –
1. Find documents where “uom” value is “in”
db.inventory.find( { "size.uom": "in" } )
2. Find documents based on multiple nested fields
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
Example 2:
Insert array field documents
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 ] }
]);
• Match an Array
• To specify equality condition on an array, provide exact value of array field
• Example –
db.inventory.find( { tags: ["red", "blank"] } )
• Query an array for an Element
• To query an array field contains at least one element with the specified value
Example –
1. db.inventory.find( { tags: "red" } )
2. db.inventory.find( { dim_cm: { $gt: 25 } } )
Example 3:
Insert array of Embedded Documents
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
Example –
Query array of embedded document “instock” with matching “sub-document”
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
• Return all fields in the matching documents
• If you don’t specify projection document, all fields will be returned
Example –
db.inventory.find( { status: "A" } )
Suppress _id field
• By default, _id field returns even if you specify the project document
• To suppress _id field, you need to specify “_id” with 0 in projection document
• Example –
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
• Return all but excluded fields
• You can return all fields, but suppress unwanted fields
• Example –
• db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
Comparison Operators : $eq, $ne, $in, $nin
Examples:
Comparison Operators - $eq
• Equals a specified value
1). db.inventory.find( { qty: { $eq: 20 } } )
• Equals a specified value in Embedded document
2). db.inventory.find( { "item.name": { $eq: "ab" } } )
• Equals a specified value in Array
3). db.inventory.find( { tags: { $eq: "B" } } )
Example :
Comparison Operators - $ne
• Non-equal to specified value
1). db.inventory.find( { qty: { $ne: 20 } } )
• Non-Equal to specified value in Array
2). db.inventory.find( { tags: { $ne: "B" } } )
Example :
Comparison Operators - $in , $nin
• Match values in the array
1).db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
• Match with the regular expression
db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )
Note: Starts with ‘be’ and ‘st’
• Match documents if not in the specified array
db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
Example :
Comparison Operators : $gt, $gte, $lt, $lte
Comparison Operators - $gt , $gte
• $gt
• Example –
db.inventory.find( { qty: { $gt: 20 } } )
• $gte
• Example –
db.inventory.find( { qty: { $gte: 20 } } )
Comparison Operators - $lt , $lte
• $lt
• Example –
db.inventory.find( { qty: { $lt: 20 } } )
• $lte
• Example –
db.inventory.find( { qty: { $lte: 20 } } )
Logical Operators - $and , $or
• AND query with multiple expressions
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
• OR query with multiple expressions
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
• AND + OR query with multiple expressions
db.inventory.find( {
$and: [
{ $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] },
{ $or: [ { sale: true }, { price : { $lt : 5 } } ] }
]
} )
Logical Operators - $not , $nor
• $not query
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
• $not with regular expression
db.inventory.find( { item: { $not: /^p.*/ } } )
• $nor query with two expressions
db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ] } )
• $nor with $exists
db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } },
{ sale: true }, { sale: { $exists: false } } ] } )
Element Operators - $exists
• Insert null data for $exists
db.records.insertMany([
{ a: 5, b: 5, c: null },
{ a: 3, b: null, c: 8 },
{ a: null, b: 3, c: 9 },
{ a: 1, b: 2, c: 3 },
{ a: 2, c: 5 },
{ a: 3, b: 2 },
{ a: 4 },
{ b: 2, c: 4 },
{ b: 2 },
{ c: 6 }
])
Element Operators - $exists
• Exists and not equal to
db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )
• Exists with null values
db.records.find( { a: { $exists: true } } )
Or
db.records.find( { b: { $exists: false } } )
Element Operators - $type
• Insert types data for $type
db.addressBook.insertMany(
[
{ "_id" : 1, address : "2030 Martian Way", zipCode : "90698345", "readings": [ 25, 25, 24, 23] ,
classAverage : NumberInt("93") },
{ "_id" : 2, address: "156 Lunar Place", zipCode : 43339374, "readings": [ 22, 24, [] ], classAverage
: 87.333333333333333 },
{ "_id" : 3, address : "2324 Pluto Place", zipCode: NumberLong(3921412), "readings": 24,
classAverage : "83.52" },
{ "_id" : 4, address : "55 Saturn Ring" , zipCode : NumberInt(88602117), "readings": [],
classAverage: "94.06" },
{ "_id" : 5, address : "104 Venus Drive", zipCode : ["834847278", "1893289032"], "readings":
"done"}
]
)
Element Operators - $type
• Querying by data types
db.addressBook.find( { "zipCode" : { $type : "string" } } )
• Querying by multiple data types
db.addressBook.find( { "classAverage" : { $type : [ "string" , "double" ] } } )
• Querying by Array Type
db.addressBook.find( { "readings" : { $type: "array" } } )
Evaluation Operators - $regex
• Insert data –
db.products.insertMany([
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." },
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" },
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" },
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
])
• Perform like match
db.products.find( { sku: { $regex: /789$/ } } )
Evaluation Operators - $regex
• Perform case-insensitive match
db.products.find( { sku: { $regex: /^ABC/i } } )
• Multiline match for lines starting with specified patterns
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
• Ignoring white spaces and comments in Pattern
var pattern = "abc #category code\n123 #item number"
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
No comments:
Post a Comment