show dbs;
use myschool;
db;
db.dropDatabase();
show collections;
db.createCollection("student");
db.student.drop();
db.student.insert({ name: "张三", age: 18, money: 12000.00 });
db.student.insert({ name: "李四", age: 20, money: 12000.00 });
db.student.insert({ name: "王五", age: 25, money: 12000.00 });
db.student.insert({ name: "老刘", age: 3, money: 12000.00 });
db.student.insert({ name: "七七", age: 52, money: 12000.00 });
db.student.insert({ name: "钱二", age: 33, money: 12000.00 });
db.student.update(
{ name: "张三" },
{ $set: { age: 20 } }
)
db.student.update(
{ uname: "张三" },
{ $set: { age: 20 } },
{ upsert: true }
)
db.student.update(
{ name: "张三" },
{ $set: { name: "张三丰" } },
{ multi: true }
)
db.student.update(
{ uname: "张三" },
{ $inc: { age: NumberInt(-20) } }
)
{}占位符 代表全部
db.student.remove({})
db.student.remove({ name: "张三丰" })
db.student.remove(
{ name: "张三丰" },
{ justOne: true }
)
db.student.find()
db.student.find({ name: "张三" })
db.student.find({ age: { $lt: 20 } });
db.student.find({ age: { $lte: 20 } });
db.student.find({ age: { $gt: 20 } });
db.student.find({ age: { $gte: 20 } });
db.student.find({ age: { $ne: 20 } });
db.student.find(
{
name: { $ne: "张三" },
age: { $lt: 20 }
}
);
db.student.find(
{
$or: [
{ name: { $ne: "张三" } },
{ age: { $lte: 20 } }
]
}
);
db.student.find({age: { $gte: 20 , $lte: 50} });
db.student.find({name:/张/});
db.student.find({name:/^张/});
db.student.find({name:/张$/});
db.student.find().limit(3).skip((2-1)*3)
db.student.count({age})
db.student.find().sort({age:1})
db.student.getIndexes()
db.student.createIndex({name:1});
db.student.createIndex({name:-1});
db.student.dropIndex({name:-1});