db.Shirts.remove({});
db.Shirts.insertMany(
[
{
Brand:"Calvin",
Name: "Name 1",
Color: ["Blue", "Red"],
Price: 100,
Size: {
height:10, width:5
}
},
{
Brand:"Tommy",
Name: "Name 2",
Color: ["Blue", "Red", "Orange"],
Price: 10,
Size: {
height:10, width:5
}
}
,
{
Brand:"Calvin Kleins",
Name: "Name 3",
Color: ["Blue", "Red", "Orange"],
Price: 50,
Size: {
height:10, width:5
}
}
]
);
// related to: Update the Brand to Calvin Klein when you see only Calvin
db.Shirts.find( {Brand:"Calvin"} );
db.Shirts.find({Brand:{regex:/Calvin/}});
db.Shirts.find({Brand:"Calvin"});
db.Shirts.find({Brand:"Tommy"});
db.Shirts.updateMany({Brand:"Calvin"}, {$set:{Brand:"Calvin Klein"}} );
db.Shirts.find({});
db.Shirts.find(
{
Brand:"Calvin"
}
);
db.Shirts.find( {
Brand:{
$gt:2
}
}
);
// https://www.mongodb.com/docs/manual/reference/operator/query/regex/
db.Shirts.find({
Brand: { $regex: /Cal/ }
});
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
Price:
{
$gte:50, $lte:100
}
}
);
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
$and:[
{Price:{$gte:50}},
{Price:{$lte:100}}
]
}
);
// Find shirts where the color is Blue or Red
db.Shirts.find (
{
Color: {$in:["Red", "Blue"]}
}
);
db.Shirts.find({"_id" : ObjectId("660191c185c168d7ae91df2f")});
db.collection.findById('4ecbe7f9e8c1c9092c000027');
let id = '660191c185c168d7ae91df2f';
let o_id = new ObjectId(id);
db.Shirts.findOne({"_id":o_id});
db.runCommand(
{
create: "Shirts",
clusteredIndex: {
"key": { _id: 1 },
"unique": true,
"name": "ShirtsClusteredKey"
}
}
);
db.movies.aggregate([
{
$search: {
index: "default", // optional unless you named your index something other than "default"
text: {
query: "star wars",
path: "title"
},
},
},
{
$project: {
title: 1,
year: 1,
}
}
])
db.runCommand( {
create: "Shirts",
clusteredIndex: { "key": { price: 1 }, "unique": true, "name": "products clustered key" }
} )
db.Shirts.createIndex( { Price:1 } )
db.Shirts.aggregate([
{
$search: {
index: "Price", // optional unless you named your index something other than "default"
text: {
query: "Tommy",
path: "Brand"
},
},
},
{
$project: {
Brand: 1,
Price: 1,
}
}
])
db.Shirts.getIndexes()
db.Shirts.find({Price:{$gt:10}});
var shrt = db.Shirts.find({});
while (shrt.hasNext() ) {
print(tojson(shrt.next()))
}
var shrt = db.Shirts.find({});
print(shrt);
while (shrt.hasNext() ) {
print(shrt.next())
}
db.Reviews.aggregate([
{
$lookup: {
from: "Products",
localField: "product_id",
foreignField: "_id",
as: "movie_details",
},
},
{
$limit: 1
}
])
Aug 07