MongoDB切片数组在填充字段?
> db.demo503.insertOne({_id:1,Name:"John",Subject:["MySQL","Java","C"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo503.insertOne({_id:2,Name:"David",Subject:["MongoDB","C++","Python"]}); { "acknowledged" : true, "insertedId" : 2 }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo503.find().pretty();
这将产生以下输出-
{ "_id" : 1, "Name" : "John", "Subject" : [ "MySQL", "Java", "C" ] } { "_id" : 2, "Name" : "David", "Subject" : [ "MongoDB", "C++", "Python" ] }
以下是对填充字段中的切片数组的查询-
> db.demo503.find({_id:2}, { 'Subject': { $slice: -1 }});
这将产生以下输出-
{ "_id" : 2, "Name" : "David", "Subject" : [ "Python" ] }