以下代码/命令可直接复制粘贴

第一关 数据的导入导出

mongoimport -d mydb1 -c score --type csv --headerline --ignoreBlanks --file /home/example/student.csv
mongoexport -d mydb1 -c score -o /home/test1.json --type json
mongoexport -d mydb1 -c score -o/home/test1.csv --type csv -f "_id,name,age,sex,major"

第二关 高级查询(一)

mongoimport -d mydb2 -c test --type json --file /home/example/person.json

db.test.find({hobbies:{\$all:['唱歌','跳舞']}}).sort({_id:1});
db.test.find({hobbies:{\$all:['羽毛球','跳舞']}}).sort({_id:1});
db.test.find({hobbies:{\$size:3}}).sort({_id:1});
db.test.find({hobbies:{\$exists:true}}).sort({_id:1});
db.test.find({age:{\$in:[19,23]}}).sort({_id:1});
db.test.find({age:{\$nin:[20]}}).sort({_id:1});
db.test.find({age:{\$mod:[9,2]}}).sort({_id:1});

第三关 高级查询(二)

mongoimport -d mydb3 -c test --type json --file /home/example/person.json

db.test.find({\$and:[{age:20},{sex:'男'}]}).sort({_id:1});
db.test.find({\$or:[{age:20},{sex:'男'}]}).sort({_id:1});
db.test.find({name:/^韩.*/}).sort({_id:1});
db.test.find({\$and:[{age:{\$gte:19}},{age:{\$lt:22}}]}).sort({_id:1});
db.test.find({\$or:[{age:{\$lt:19}},{age:{\$gt:21}}]}).sort({_id:1});
db.test.find({name:{\$not:/^韩.*/}}).sort({_id:1});
db.test.find({name:{\$not:/^韩.*/}}).count();
db.test.find({\$and:[{age:{\$gte:19}},{age:{\$lt:22}}]}).count();

第四关 游标

mongo
use mydb4
for(var i=0;i<10000;i++)db.test.insert({_id:i,title:"MongoDB"+i,content:"hello"+i})
var cursor=db.test.find({_id:{$lte:5}})
exit

mongoexport -d mydb4 -c test -o /home/test/test4.csv --type csv -f "_id,title,content"
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐