MongoDB切片数组在填充字段?
要切片数组,请在MongoDB中使用$slice运算符。让我们创建一个包含文档的集合-
> 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" ] }
以上是 MongoDB切片数组在填充字段? 的全部内容, 来源链接: utcz.com/z/359244.html