var tags = ["nosql","mongodb","document","developer","popular"];
var types = ["technology","sociality","travel","novel","literature"];
var books=[];
for(var i=0;i<50;i++){
var typeIdx = Math.floor(Math.random()*types.length);
var tagIdx = Math.floor(Math.random()*tags.length);
var tagIdx2 = Math.floor(Math.random()*tags.length);
var favCount = Math.floor(Math.random()*100);
var username = "xx00"+Math.floor(Math.random()*10);
var age = 20 + Math.floor(Math.random()*15);
var book = {
title: "book-"+i,
type: types[typeIdx],
tag: [tags[tagIdx],tags[tagIdx2]],
favCount: favCount,
author: {name:username,age:age}
};
books.push(book)
}
db.books1.insertMany(books);
> db.books1.find()
> db.books1.find().pretty()
db.books1.aggregate([{$project:{name:"$title"}}])
//_id:0表示不显示id字段;type:1表示显示type字段;author:1表示显示author字段
db.books1.aggregate([{$project:{name:"$title",_id:0,type:1,author:1}}])
db.books1.aggregate([ {$project:{name:"$title",_id:0,type:1,"author.name":1}} ])
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wwwxz.blog.csdn.net/article/details/124334246
内容来源于网络,如有侵权,请联系作者删除!