'find document in mongo by a pecif field
I have he following schema in mongodb:
I am trying to get all documents by a certain organization as set in the regex, but I do not get the proper filtered list.
I have also tried geopoints.find(organization:"abc123"). Not sure why I am not getting a filtered list.
const promise2 = geopoints.find({organization: {$regex: 'abc123'}}, {locations:1, organization:1, _id:0})
Here is the full code:
app.get('/onlyfields', (req, res) => {
//onlyfields.find()
//.then(data => res.send({fields: data, rows: [[2, '2015-01-15 14:00:41 +00:00', '2015-01-15 14:11:18 +00:00', 1, 1.64, -73.9837265, 40.74634171, -73.96679688, 40.76140594, 8.5, 0, 9.3], [2, '2015-01-15 14:00:41 +00:00', '2015-01-15 14:19:26 +00:00', 1, 1.53, -73.99578094, 40.73294067, -73.99107361, 40.75038147, 12, 0, 12.8]]}))
//FIND ALL CONSULTATIONS FILTERED BY STATUS
const promise1 = onlyfields.find({}, {_id:0})
const promise2 = geopoints.find({organization: 'abc123'}, {locations:1, organization:1, _id:0})
Promise.all([promise1, promise2])
.then(([result1, result2]) => {
const flatData = result2.map(currEntry => currEntry.locations)
res.send({ fields: result1, rows: flatData })
//console.log(flatData)
})
.catch(err => {
return res.status(400).json({
error: 'Erorr en STATUS2'
})
})
})
Solution 1:[1]
Call toArray() to convert cursor to an Array promise
find().toArray()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

