本文所有题目来自头歌学习平台https://www.educoder.net/paths/xe4uogqs
代码是自己写的
数据
bash
var document = [
{ "_id": 1, "name": "西西", "sex": "女", "age": 23, "national": "汉族" },
{ "_id": 2, "name": "东东", "sex": "男", "age": 20, "national": "苗族" },
{ "_id": 3, "name": "北北", "sex": "男", "age": 19, "national": "汉族" },
{ "_id": 4, "name": "南南", "sex": "女", "age": 15, "national": "傣族" }
];
要求
把上述数据赋值给变量 document (一条命令),插入到集合 stu1 、stu2;
执行查询命令,查找集合 stu1 中年龄大于等于15岁的女生;
执行查询命令,查找集合 stu1 中苗族人;
执行查询命令,查找集合 stu1 中年龄小于20岁的男生;
执行删除命令,删除集合 stu2 的所有数据。
题目的答案
bash
`var document = [
{ "_id": 1, "name": "西西", "sex": "女", "age": 23, "national": "汉族" },
{ "_id": 2, "name": "东东", "sex": "男", "age": 20, "national": "苗族" },
{ "_id": 3, "name": "北北", "sex": "男", "age": 19, "national": "汉族" },
{ "_id": 4, "name": "南南", "sex": "女", "age": 15, "national": "傣族" }
];
db.stu1.insertMany(document);
db.stu2.insertMany(document);
db.stu1.find({ age: { $gte: 15 }, sex: "女" });
db.stu1.find({ national: "苗族" });
db.stu1.find({ age: { $lt: 20 }, sex: "男" });
db.stu2.deleteMany({});
`