如何限制从MongoDB中的字段返回的字符数?
要限制从字段返回的字符数,请在MongoDB中使用$substr。让我们创建一个包含文档的集合-
> db.demo233.insertOne({"Paragraph":"My Name is John Smith.I am learning MongoDB database"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e41877df4cebbeaebec5146")
}
> db.demo233.insertOne({"Paragraph":"David Miller is a good student and learning Spring and Hibernate Framework."});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e4187d7f4cebbeaebec5147")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo233.find().pretty();
这将产生以下输出-
{
"_id" : ObjectId("5e41877df4cebbeaebec5146"),
"Paragraph" : "My Name is John Smith.I am learning MongoDB database"
}
{
"_id" : ObjectId("5e4187d7f4cebbeaebec5147"),
"Paragraph" : "David Miller is a good student and learning Spring and Hibernate Framework."
}以下是查询以限制从MongoDB中的字段返回的字符数-
> db.demo233.aggregate(
... [
... {
... $project:
... {
... Paragraph: { $substr: [ "$Paragraph", 0, 10] }
...
... }
...} ] )这将产生以下输出-
{ "_id" : ObjectId("5e41877df4cebbeaebec5146"), "Paragraph" : "My Name is" }
{ "_id" : ObjectId("5e4187d7f4cebbeaebec5147"), "Paragraph" : "David Mill" }热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短