如何使用 NodeJS 仅将 NeDB 数据库中的某些字段存储到数组中
How to store only certain fields from NeDB database into an array with NodeJS
我有一个嵌入式 NeDB 数据库,其中包含多个条目和多个字段,我只想从每个条目中获取日期并将其存储到一个数组中。我正在使用 NeDB、NodeJS 和 express。
数据集如下所示:
{"goalName":"swim 5km","details":"I want to swim 5km","date":"2021-05-15","completed":false,"author":"somename","_id":"BMnvTm54rNbwc9D4"}
{"goalName":"swim 5km","details":" I want to swim another 5km","date":"2021-03-14","completed":false,"author":"somename","_id":"EwEicEYZAfFxY9Z6"}
{"goalName":"20 pushups","details":"I want to complete 20 full pushups","date":"2021-05-14","completed":false,"author":"anthername","_id":"rP7q6L8jnwGyAgGD"}
我只对作者姓名的日期感兴趣,
我可以使用以下方法检索这些文档:
getEntriesByUser(userName) {
return new Promise((resolve, reject) => {
this.db.find({ 'author': userName }, function (err, entries) {
if (err) {
reject(err);
} else {
resolve(entries);
console.log('getEntriesByUser returns: ', entries);
}
})
})
}
然后 returns 用户名 = 某个用户名的文档,但我只对日期感兴趣。最好将它们存储到一个数组中,结果如下:
[2021-05-15, 2021-03-14, 2021-05-14]
我怎么会这样做?
感谢您的帮助!
您可以使用 find()
和 findOne()
方法的可选第二个 projection
参数来包含或省略返回记录的属性。参见:NeDB#projections.
db.find({ author: userName }, { date: 1, _id: 0 }, function (err, docs) {...});
const
Datastore = Nedb,
db = new Datastore(),
data = [
{ goalName: "swim 5km", details: "I want to swim 5km", date: "2021-05-15", completed: false, author: "somename" },
{ goalName: "swim 5km", details: " I want to swim another 5km", date: "2021-03-14", completed: false, author: "somename" },
{ goalName: "20 pushups", details: "I want to complete 20 full pushups", date: "2021-05-14", completed: false, author: "anthername" },
];
for (const datum of data) {
db.insert(datum);
}
function getEntriesByUser(userName) {
return new Promise((resolve, reject) => {
db.find({ author: userName }, { date: 1, _id: 0 }, function (err, entries) {
if (err) {
reject(err);
} else {
resolve(entries);
console.log('getEntriesByUser returns: ', entries);
}
})
})
}
getEntriesByUser('somename').then((entries) => {
console.log('Mapped return value: ', entries.map(({ date }) => date));
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/nedb/1.8.0/nedb.min.js"></script>
我有一个嵌入式 NeDB 数据库,其中包含多个条目和多个字段,我只想从每个条目中获取日期并将其存储到一个数组中。我正在使用 NeDB、NodeJS 和 express。
数据集如下所示:
{"goalName":"swim 5km","details":"I want to swim 5km","date":"2021-05-15","completed":false,"author":"somename","_id":"BMnvTm54rNbwc9D4"}
{"goalName":"swim 5km","details":" I want to swim another 5km","date":"2021-03-14","completed":false,"author":"somename","_id":"EwEicEYZAfFxY9Z6"}
{"goalName":"20 pushups","details":"I want to complete 20 full pushups","date":"2021-05-14","completed":false,"author":"anthername","_id":"rP7q6L8jnwGyAgGD"}
我只对作者姓名的日期感兴趣,
我可以使用以下方法检索这些文档:
getEntriesByUser(userName) {
return new Promise((resolve, reject) => {
this.db.find({ 'author': userName }, function (err, entries) {
if (err) {
reject(err);
} else {
resolve(entries);
console.log('getEntriesByUser returns: ', entries);
}
})
})
}
然后 returns 用户名 = 某个用户名的文档,但我只对日期感兴趣。最好将它们存储到一个数组中,结果如下:
[2021-05-15, 2021-03-14, 2021-05-14]
我怎么会这样做?
感谢您的帮助!
您可以使用 find()
和 findOne()
方法的可选第二个 projection
参数来包含或省略返回记录的属性。参见:NeDB#projections.
db.find({ author: userName }, { date: 1, _id: 0 }, function (err, docs) {...});
const
Datastore = Nedb,
db = new Datastore(),
data = [
{ goalName: "swim 5km", details: "I want to swim 5km", date: "2021-05-15", completed: false, author: "somename" },
{ goalName: "swim 5km", details: " I want to swim another 5km", date: "2021-03-14", completed: false, author: "somename" },
{ goalName: "20 pushups", details: "I want to complete 20 full pushups", date: "2021-05-14", completed: false, author: "anthername" },
];
for (const datum of data) {
db.insert(datum);
}
function getEntriesByUser(userName) {
return new Promise((resolve, reject) => {
db.find({ author: userName }, { date: 1, _id: 0 }, function (err, entries) {
if (err) {
reject(err);
} else {
resolve(entries);
console.log('getEntriesByUser returns: ', entries);
}
})
})
}
getEntriesByUser('somename').then((entries) => {
console.log('Mapped return value: ', entries.map(({ date }) => date));
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/nedb/1.8.0/nedb.min.js"></script>