So this is how my database looks right now:
{
_id": "r8uoPSvJY36nHgCK9",
"name": "Running",
"category": "leisure",
"duration": "2",
"createdAt": "2/10 16:42:17",
"skills": {
"creativity": 6,
"analytics": 3,
"fitness": 7,
"research": 4,
"communication": 4,
"problemSolving": 3,
"timeManagement": 7,
"leadership": 3,
"selfMotivation": 3,
"teamwork": 4
},
"started": "false",
"finished": "false"
}
How can I query the collection so that I get the value of the creativity field and store it in a variable? I have tried something like:
tasks.find({'skills.creativity': this._id});
but it doesn't appear to work.
.find()
returns a cursor - ie all the matching records, not just the key that you're looking for.
If you're looking for the creativity field for a single record and assuming you are trying to find the document by its _id
and not by the value of the skills.creativity
field (which as @Styx points out would be silly) then:
const creativity = tasks.findOne(this._id).skills.creativity;
If you're looking to get the creativity field for all the matching records then:
const creativityArray = tasks.find(query).map((e) => e.skills.creativity);
where query
defines the set of documents you're looking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.