You are on page 1of 2

1,

db.zips.find({})
2,
db.mycollection.insertOne({ name: "Bob", age: 30, email: "bob@example.com" })
3,
db.zips.find({city: "PALMER"})
4,
db.zips.find({pop:{$gt:1000}})
5,
db.zips.find({city: "FISHERS ISLAND"}, { _id: 0, pop: 1 })

6,
db.zips.find({pop: { $gte: 10, $lte: 50 }})

7,
db.zips.find({
state: "MA",
pop: { $gt: 500 }
})
8,
db.zips.distinct("state")

9,
var distinctStates = db.zips.distinct("state")
for (var i = 0; i < distinctStates.length; i++) {
var state = distinctStates[i];
var cityWithPopulationOver100000 = db.zips.findOne({ state: state, pop: { $gt: 100000 } });
if (cityWithPopulationOver100000) {
print("Bang " + state + " có ít nhất một thành phố có dân số trên 100,000.");
}
}
10,
db.zips.aggregate([
{
$group: {
_id: "$state",
averagePop: { $avg: "$pop" }
}
}
])

11,

db.zips.estimatedDocumentCount({ state: "WP" }, function (err, count) {


if (err) {
console.log(err);
} else {
console.log("Số lượng thành phố trong bang WP là: " + count);
}
});
12,
db.zips.aggregate([
{
$group: {
_id: "$state",
totalCities: { $sum: 1 }
}
}
])
13,
db.zips.aggregate([
{
$group: {
_id: "$state",
totalPop: { $sum: "$pop" }
}
},
{
$match: {
totalPop: { $gt: 10000000 }
}
}
])

You might also like