Skip to content

Instantly share code, notes, and snippets.

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",
db.orders.runCommand("collMod",
{validationLevel: "strict",
validationAction: "error"});
db.orders.update(
{price:"free"},
{$set: {price: 0}},
{multi: true});
db.orders.update(
{price:"if you have to ask...."},
{$set: {price: 1000000}},
{multi: true});
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 }
db.orders.findOne({price: {$type: 2}});
{
"_id": 3500,
"item": "abc",
"price": "free",
"quantity": 5
}
> db.orders.update(
db.orders.insert({
"_id": 6666,
"item": "jkl",
"price": "rogue",
"quantity": 1 });
Document failed validation
WriteResult({
"nInserted": 0,
"writeError": {
db.orders.runCommand("collMod",
{validationLevel: "moderate",
validationAction: "error"});
db.runCommand({collMod: "orders",
validator: {
price: {$exists: true},
price: {$type: 1}
}
});
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",
db.runCommand({
collMod: "contacts",
validator:
{$or: [{version: {"$exists": false}},
{version: 1,
$and: [{Name: {"$exists": true}}]
},
{version: 2,
$and: [{Name: {"$exists": true, "$type": 2}}]
}
db.runCommand({
collMod: "contacts",
validator: {
$and: [
{year_of_birth: {$lte: 1994}},
{$or: [
{phone: { $type: "string"}},
{email: { $type: "string"}}
]}]
}})