Azure functions:
module.exports = async function (context, req) {
await client.connect();
const database = client.db("crud");
const collection = database.collection("moments");
let obj = await collection.aggregate([{$sample:{ size: 3 }}]).toArray();
if (!obj) {
return context.res = {
status: 400,
body: "not found"
};
}
return context.res = {
status: 200, /* Defaults to 200 */
body: obj
};
}
I am getting the same records each time (first 3) from the db.collection
.
If query without the toArray()
method, returns:
{
"_events": {},
"_eventsCount": 0
}
which I can't seem to find why.