This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.homeSales.findOne() | |
{ | |
"_id": ObjectId("56005dd980c3678b19792b7f"), | |
"amount": 9000, | |
"date": ISODate("1996-09-19T00:00:00Z"), | |
"address": { | |
"nameOrNumber": 25, | |
"street": "NORFOLK PARK COTTAGES", | |
"town": "MAIDENHEAD", | |
"county": "WINDSOR AND MAIDENHEAD", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.runCommand("collMod", | |
{validationLevel: "strict", | |
validationAction: "error"}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.update( | |
{price:"free"}, | |
{$set: {price: 0}}, | |
{multi: true}); | |
db.orders.update( | |
{price:"if you have to ask...."}, | |
{$set: {price: 1000000}}, | |
{multi: true}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
secondary> db.orders.aggregate([ | |
{$match: { | |
price: {$type: 2}}}, | |
{$group: { | |
_id: "$price", | |
count: {$sum:1}}} | |
]) | |
{ "_id" : "if you have to ask....", "count" : 250 } | |
{ "_id" : "free", "count" : 500 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.findOne({price: {$type: 2}}); | |
{ | |
"_id": 3500, | |
"item": "abc", | |
"price": "free", | |
"quantity": 5 | |
} | |
> db.orders.update( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.insert({ | |
"_id": 6666, | |
"item": "jkl", | |
"price": "rogue", | |
"quantity": 1 }); | |
Document failed validation | |
WriteResult({ | |
"nInserted": 0, | |
"writeError": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.runCommand("collMod", | |
{validationLevel: "moderate", | |
validationAction: "error"}); | |
db.runCommand({collMod: "orders", | |
validator: { | |
price: {$exists: true}, | |
price: {$type: 1} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use clusterdb; | |
db.dropDatabase(); | |
use clusterdb(); | |
db.inventory.insert({ "_id" : 1, "sku" : "abc", | |
"description" : "product 1", "instock" : 120 }); | |
db.inventory.insert({ "_id" : 2, "sku" : "def", | |
"description" : "product 2", "instock" : 80 }); | |
db.inventory.insert({ "_id" : 3, "sku" : "ijk", | |
"description" : "product 3", "instock" : 60 }); | |
db.inventory.insert({ "_id" : 4, "sku" : "jkl", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.runCommand({ | |
collMod: "contacts", | |
validator: | |
{$or: [{version: {"$exists": false}}, | |
{version: 1, | |
$and: [{Name: {"$exists": true}}] | |
}, | |
{version: 2, | |
$and: [{Name: {"$exists": true, "$type": 2}}] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.runCommand({ | |
collMod: "contacts", | |
validator: { | |
$and: [ | |
{year_of_birth: {$lte: 1994}}, | |
{$or: [ | |
{phone: { $type: "string"}}, | |
{email: { $type: "string"}} | |
]}] | |
}}) |