Pushing to array which is in an array of objects - mongodb

{
"_id": {
"$oid": "5bd8fa85bfbf4e27d86c73eb"
},
"status": {
"iat": 1540947201356,
"exp": 1540950801358
},
"code": "1cmTwKC7",
"mongoDate": {
"$date": "2018-10-31T00:41:56.016Z"
},
"form": [
{
"type": "tf",
"text": "The first form ever. Welcome...",
"opts": [],
"responses": []
}
],
"updateDate": "October 30th, 2018, 8:47 PM",
"date": "2019",
"notes": "notes for this lecture.",
"name": "TEST",
"user": {
"$oid": "5bcfd8a8da59d54024172d5c"
},
"course": {
"$oid": "5bd8fa6cbfbf4e27d86c73ea"
},
"__v": 1
}
I have a document which is of the form above. I wanted to push a value into the responses array of a document which is in the form array. Im not sure how to do this using mongoose for MongoDB, and havent been able to find a direct answer. Thanks in advance.

Related

Enrich array of objects with another document based on a condition

Given two collections users and activities, I want to enrich users.friends.user with all relevant activities, such that users.friends.user matches doc2.invitations.userId.
Below describes exactly what I would like:
user f24b189f-9e00-4d2b-b7f2-148a265fae7c is not a part of any activity, so their activities array is [].
user 223f2e04-6f0c-40a3-a88a-69d0127f2e92 is a part of an activity, so their activities array is populated with the activity object.
Raw:
db={
"users": [
{
"_id": "9c69a57f-e90a-43be-b559-449e973c6cba",
"__v": {
"$numberInt": "0"
},
"friends": [
{
"status": "ACCEPTED",
"user": "f24b189f-9e00-4d2b-b7f2-148a265fae7c"
},
{
"status": "ACCEPTED",
"user": "223f2e04-6f0c-40a3-a88a-69d0127f2e92"
}
]
}
],
"activities": [
{
"_id": {
"$oid": "63a92531bc6dea668f93bb06"
},
"invitations": [
{
"_id": {
"$oid": "63a92531bc6dea668f93bb07"
},
"userId": "9c69a57f-e90a-43be-b559-449e973c6cba",
"status": "ACCEPTED"
},
{
"_id": {
"$oid": "63a92544bc6dea668f93bb09"
},
"userId": "223f2e04-6f0c-40a3-a88a-69d0127f2e92",
"status": "ACCEPTED"
},
],
"__v": {
"$numberInt": "0"
}
}
]
}
Expected result:
"expectedResult": [
{
"_id": "9c69a57f-e90a-43be-b559-449e973c6cba",
"__v": {
"$numberInt": "0"
},
"friends": [
{
"status": "ACCEPTED",
"activities": [{
"_id": {
"$oid": "63a92531bc6dea668f93bb06"
},
"invitations": [
{
"_id": {
"$oid": "63a92531bc6dea668f93bb07"
},
"userId": "9c69a57f-e90a-43be-b559-449e973c6cba",
"status": "ACCEPTED"
},
{
"_id": {
"$oid": "63a92544bc6dea668f93bb09"
},
"userId": "223f2e04-6f0c-40a3-a88a-69d0127f2e92",
"status": "ACCEPTED"
},
],
"__v": {
"$numberInt": "0"
}
}]
},
{
"status": "ACCEPTED",
"activities": []
}
]
}
]
And here's a link to the playground.
I played around with aggregate and populate, but this use case is unlike a regular "join", which is throwing me off. Any pointers will be appreciated.

finding document based on different input mongodb

let bondNumber1 = "100000000001"
let bondNumber2 = "00000001"
let bondNumber3 = "000001"
let bondNumber4 = "0001"
I need to choose the user whose contain these bondNumber in their data and I want them separately, like the user contain bondNumber1 value retreived separately and the user who contain bondNumber2 value retreived seperatly and so on.
[{
"_id": {
"$oid": "601af5cadea1ae126ac56bab"
},
"status": "pending",
"bondNumber": "100000000001",
"date": {
"$date": "1970-01-19T15:52:59.587Z"
},
"type": "sold"
},{
"_id": {
"$oid": "601af80fdea1ae126ac56bac"
},
"status": "pending",
"bondNumber": "100000000002",
"date": {
"$date": "1970-01-19T15:53:00.172Z"
},
"type": "sold"
},{
"_id": {
"$oid": "601afc0be3abcd1b474c546a"
},
"status": "pending",
"bondNumber": "100000000003",
"date": {
"$date": "1970-01-19T15:53:01.192Z"
},
"type": "sold"
},{
"_id": {
"$oid": "601afe7ce3abcd1b474c546b"
},
"status": "pending",
"bondNumber": "100000000004",
"date": {
"$date": "1970-01-19T15:53:01.816Z"
},
"type": "active"
}]
Try this one:
let inputBondNumbers = ["100000000001","00000001","000001","0001"]
let users = await db.collectionNameHere.find({
bondNumber: {$in : inputBondNumbers}
})

Find specific field in array by last date and updating it

I have the document below, and I want to update in exams, the last exam with status "started" (according to date), I need to add to "done" another number
{
"_id": ObjectID("5d2a371cec7eaf00119df614"),
"firstname": "Laura",
"lastname": "Warriner",
"image": "2019-07-14t05:25:23.352z_13.jpg",
"exams": [
{
"examId": "Ba6apmRmz",
"name": "general",
"language": "es",
"version": 2,
"testId": "2000",
"status": [
{
"status": "created",
"date": ISODate("2019-09-08T11:49:42.124Z")
},
{
"status": "started",
"date": ISODate("2019-09-08T11:55:09.873Z"),
"done": [1]
},
{
"status": "started",
"date": ISODate("2019-09-09T12:01:57.886Z"),
"done": [2,3]
},
{
"status": "completed",
"date": ISODate("2019-09-09T12:01:57.886Z")
}
]
}
]
}
Thanks in advance

Find and Update Inner Array - MongoDB

{
"_id": {
"$oid": "5a99553c1b4a1b0922039e73"
},
"date_added": {
"$date": "2018-03-02T13:42:36.684Z"
},
"title": "Black Panther",
"__v": 14,
"alls": [
{
"name": "Mbezi",
"geocoordinates": "-6.8140361,39.2775983",
"region": "Dar es salaam",
"times": "",
"_id": {
"$oid": "5a9c74885980731a35a013b2"
}
},
{
"name": "DFM",
"geocoordinates": "-6.7726935,39.2196418",
"region": "Dar es salaam",
"times": "",
"_id": {
"$oid": "5a9c7f61bb57291a81d8309e"
}
}
]
}
How can i find alls name equals to "Mbezi" and update times to a certain value? Any links that can help me will be appreciated..

MongoDB EmbedsMany Query

So I'm using loopback to create a RESTful node.js API. I'm using mLab for my database. I'm trying to query data that is embedded inside of my Account model.
An Account model has a property called favorites which has an array of Songs.
Here is an example:
{
"_id": {
"$oid": "58512c1b7c05c491b2cff1f6"
},
"firstName": "Matt",
"lastName": "Thomas",
"username": "mattietea",
"password": "$2a$10$dcquPHK48H5Q7FcaTyzgf.vnrUOgKpBENS9fSa0hjoaASs0uyOWba",
"email": "mattcthomas#me.com",
"_favorites": [
{
"title": "Ellusive & Eljay - Like This Before...",
"artist": "Ellusive",
"audio": "https://api.soundcloud.com/tracks/236100069/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000145902859-5467c0-t500x500.jpg",
"download": "https://api.soundcloud.com/tracks/236100069/download",
"url": "https://soundcloud.com/iamellusive/like-this-before",
"created": {
"$date": "2016-12-13T19:00:50.136Z"
},
"genres": {
"hipHop": 100
},
"id": "5850456247ef7e110099748b"
},
{
"title": "Dazed and Confused",
"artist": "Sir Skiff",
"audio": "https://api.soundcloud.com/tracks/103322663/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000054154948-m9cwii-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/skiffmusic/dazed-and-confused",
"created": {
"$date": "2016-12-13T08:49:59.128Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb637fbf7df995f4341d3"
},
{
"title": "Runnat",
"artist": "Rory Fresco",
"audio": "https://api.soundcloud.com/tracks/258497600/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000157594908-cu10q4-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/roryxxx/runnat-2",
"created": {
"$date": "2016-12-13T08:50:52.397Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb66cfbf7df995f4341d4"
},
{
"title": "Breathe (Prod. by Yuri Beat$)",
"artist": "Chiddy Bang",
"audio": "https://api.soundcloud.com/tracks/106182458/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000055716908-f9qdau-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/chiddybang/breathe-prod-by-yuri-beat",
"created": {
"$date": "2016-12-13T08:32:36.447Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb224fbf7df995f4341ce"
}
]
}
I have an endpoint set up which returns all the favorites
api/Accounts/{id}/favorites
But I can't seem to query them. When I query all the songs from api/Songs it works but not when I query favorites.
The query I'm using is:
{'order': 'created DESC'}
I've tried multiple other queries, and none work.
Thank you!