我尝试从Mongodb集合中获取一个元素表单数组。我不需要包含所有数据的数组,我只需要其中一个元素。 所以我的数据库是这样的:
_id: ObjectId("...")
arrayName :Array
: 'string1'
: 'string2'
...
是否可以选择仅获取数组的一个元素?例如第一个。 这是我的功能,但这返回数组的所有元素。
db.collection('collectionName')
.find()
.toArray()
.then((result) => {
response.data = result;
res.json(response);
})
我尝试使用函数findOne()而不是find(),但是它不能按照我想要的方式工作。 感谢帮助。