Get all the Parent documents base on child reference Id Mongo and Nodejs - node.js

Thank you for your help.
I am scratching my head all day, I don't know I am in the right direction or not.
Problem :
I have a document [Doctor] which contains the reference [doctorSpecialities].
I have to GET ALL DOCTORS who have this id in there doctorSpecialities reference Array
Id : 5ef58dd048cdd203a0c07ba8
JSON Structure
{
"doctorSpecialities": [
"5f00cebc8bcdcd0660c12ce2",
"5ef58dd048cdd203a0c07ba8"
]
"_id": "5ef31ae80399ac05eb23e555",
"email": "signup#gmail.com",
"username": "signup#gmail.com",
"DOB": null,
"zip": null,
"phone": "12657334566",
"PMDC": "7658493",
"isVerified": false,
"aboutMe": "About Me",
"achievements": "Achievements",
"address": "padasdad",
"city": "Lahore",
"gender": "Male",
"managePractice": "Manage Practice",
"practiceGrowth": "Practice Growth",
"qualiflication": "Qualifcation",
"state": "eeeeeeee",
"workExperince": "Work Experince",
"doctorAvailability": [],
"doctorReviews": [],
"degreeCompletionYear": "2019-10-10",
"institute": "institute",
"practiceDate": "2020-10-10",
"services": "Dental"
},
Query tried
await Doctor.find({ doctorSpecialities : req.params.id})
await Doctor.find({ doctorSpecialities :{$in [ req.params.id}})
Specialty Collection
doctorCollection = Doctor.find();
doctorCollection.find({"doctorSpecialities": specialty.id})
This is how I did is it wrong?
I tried to user $Lookup but I don't know how to use it in this requirement
Please let me know if you need more information.
Thank you

If you have to get doctors details then you can use
db.collection.find({"doctorSpecialities":"5ef58dd048cdd203a0c07ba8"})
play
It returns all documents where doctorSpecialities field contains 5ef58dd048cdd203a0c07ba8

Related

How to make the mongoose data after populating

for example i have done the populate the data and i don't want the entity at which it's populated for example:
{
"gstin": "27AAATW4187E2UW",
"company_name": "TATA_AUTOCOMP ",
"status": "Hold",
"createdBy": {
"name": "xyz123",
"email": "xyz#yahoo.com",
"mobile_number": 7972512892
}
}
]
right now my data looks like above but i don't want the populated information i want my data just like that anyone help me .
{
"gstin": "27AAATW4187E2UW",
"company_name": "TATA_AUTOCOMP ",
"status": "Hold",
"name": "abcd",
"email": "xyz#gmail.com",
"mobile_number": 1234567890
}
]
in your query when use .populate you can use select and when application run your query just return selected parameters like this :
Model
.findOne({ _id: 'bogus' })
.populate('friendsRequestList', 'name email')
and if you want all result in one level in json you must edit your result with .map .forEach and ....

How to get the user information using the user lookup ID from fields property when accessing items from a share-point list using Ms Graph API

I am accessing a Share-point list using the MS graph API endpoint:
https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?expand=fields
I am getting the list items just fine, but I also want to get the user information attached in each field. The data item returned looks like this:
{
...other properties,
"fields": {
"#odata.etag": "\"eTag,1\"",
"id": "1",
"ContentType": "Item",
"Title": "<Some Title>",
"Modified": "<modified dateTime>",
"Created": "<created dateTime>",
"AuthorLookupId": "12",
"EditorLookupId": "12",
"_UIVersionString": "1.0",
"Attachments": false,
"Edit": "",
"LinkTitleNoMenu": "<num>",
"LinkTitle": "<num>",
"ItemChildCount": "0",
"FolderChildCount": "0",
"_ComplianceFlags": "",
"_ComplianceTag": "",
"_ComplianceTagWrittenTime": "",
"_ComplianceTagUserId": "",
"Status_Name": "<status_name>",
"Title0": "<some_title>",
"Dept": "Dept A",
"Emp_LeadLookupId": "200", //This is the user whose details I need(email-id)
"Quality_Approver": "<some_user>"
}
}
How do I get the user's details as well and not just a LookupId, OR how can I use the look up ID to get the said user's information?
I searched above and beyond but didn't find anything relevant. Any help is greatly appreciated!
Currently Microsoft Graph is not support the function of finding users through the lookup column.

How to delete comment which a deeply nested array of objects

I am new to MongoDB. This is a MERN project. I am using the MongoDB database. I am trying to delete the comments using id which are nested in an array of objects. My document looks like this,
{
"_id": "60aa8ed98ad79d380cb250b4",
"course_id": "609a2cedd07ba73200ada318",
"instructor_id": "609a2875aeefe40a6c9befc5",
"title": "New Annoucement",
"description": "new announcement is posted",
"video_url": "youtube.com",
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [],
"_id": "60aa912b301ccc397057ded6",
"username": "Sandy",
"user_id": "609a2875aeefe40a6c9befc5",
"content": "1",
"parentId": "60aa9123301ccc397057ded5",
"type": "reply",
"annoucementId": "60aa8ed98ad79d380cb250b4"
}
],
"_id": "60aa9123301ccc397057ded5",
"username": "Sandy",
"user_id": "609a2875aeefe40a6c9befc5",
"content": "why its not deleting",
"parentId": "60aa8f648ad79d380cb250b6",
"type": "reply",
"annoucementId": "60aa8ed98ad79d380cb250b4"
}
],
"_id": "60aa8f648ad79d380cb250b6",
"username": "Sandy",
"user_id": "609a2875aeefe40a6c9befc5",
"content": "two",
"parentId": "60aa8edd8ad79d380cb250b5",
"type": "reply",
"annoucementId": "60aa8ed98ad79d380cb250b4"
}
],
"_id": "60aa8edd8ad79d380cb250b5",
"username": "Sandy",
"user_id": "609a2875aeefe40a6c9befc5",
"content": "one",
"type": "comment",
"annoucementId": "60aa8ed98ad79d380cb250b4"
}
],
"createdAt": "2021-05-23T17:20:25.579Z",
"updatedAt": "2021-05-23T17:30:19.116Z",
"__v": 6
}
this is a big object called announcements which has a comments array and that contains an array of objects which are comments. And if someone replies to a comment I am making another array called comments and storing all the replies objects.
I want to delete the object using ._id: 60aa912b301ccc397057ded6, which is the reply object with the content "1".
I have tried to find, findById, findOneAndRemove, findOneAndDelete it's not finding the document.
You could perhaps change the way you structure the comments and not nest them? Since each comment already has a unique id, nesting them only makes it more difficult to access the comment object, and it's a rabbit's hole, where the nesting might never end?
I propose you add an property in the comment object A to point to other comment object B, if A is a reply or quote of B.
That way you can have an array of comment objects and modify them must easily.
Cheers.

i can't query over populated children attributes

I am trying to query over populated children attributes using mongoose but it straight up doesn't work and will return empty arrays all the time.
even hardcoding right and existing information as values for the query would return empty arrays.
my schema is a business schema with a 1 to 1 relationship with user schema via the attribute createdBy. the user schema has an attribute name which I am trying to query on.
so if I make a query like this :
business.find({'createdBy.name': {$regex:"steve"}}).populate('createdBy')
the above will never return any documents. although, without the find condition, everything works fine.
Can I search by the name inside a populated child or not? all tutorials say this should work fine but it just doesn't.
EDIT : an example of what the record looks like :
{
"_id": "5fddedd00e8a7e069085964f",
"status": 6,
"addInfo": "",
"descProduit": "",
"createdBy": {
"_id": "5f99b1bea9ba194dec3bd6aa",
"status": 1,
"fcmtokens": [
],
"emailVerified": 1,
"phoneVerified": 0,
"userType": "User",
"name": "steve buschemi",
"firstName": "steve",
"lastName": "buschemi",
"tel": "",
"email": "steve#buschemi.com",
"register_token": "747f1e1e8fa1ecd2f1797bb402563198",
"createdAt": "2020-10-28T18:00:30.814Z",
"updatedAt": "2020-12-18T13:52:07.430Z",
"__v": 19,
"business": "5f99b1e101bfff39a8259457",
"credit": 635,
},
"createdAt": "2020-12-19T12:10:57.703Z",
"updatedAt": "2020-12-19T12:11:16.538Z",
"__v": 0,
"nid": "187"
}
It seems there is no way to filter parent documents by conditions on child documents:
From the official documentation:
In general, there is no way to make populate() filter stories based on properties of the story's author. For example, the below query won't return any results, even though author is populated.
const story = await Story.
findOne({ 'author.name': 'Ian Fleming' }).
populate('author').
exec();
story; // null
If you want to filter stories by their author's name, you should use denormalization.

Issue on Docusign Admin Api getUserProfile

Hi I am facing an issue while loading the endpoint point /v2/organizations/{organizationId}/users/profile
I am getting a 404 error
Attached below screenshots for the postman response and getting organisationId
The correct baseUrl for demo Admin API is https://api-d.docusign.net/management/v2/
Try to change that and see if that works.
In order for that endpoint to work you're going to need to provide some additional parameters in the URL. For retrieving a user's profile directly, you should be able to use:
https://api-d.docusign.net/management/v2/organizations/{OrgID}/users/profile?email=useremail#example.com
The result looks like this:
{
"users": [
{
"id": "28fbe7e4-xxxx-xxxx-xxxx-c26569aa827c",
"site_id": 1,
"site_name": "Demo",
"user_name": "Matt_SATest_1",
"first_name": "MattSAT_1",
"last_name": "K",
"user_status": "active",
"default_account_id": "70da9c0c-xxxx-xxxx-xxxx-7b3d6733328e",
"default_account_name": "Matt K",
"is_organization_admin": false,
"created_on": "2020-08-25T20:38:57.533",
"memberships": "",
"identities": "",
"is_device_verification_enabled": true
}
]
}
To retrieve a list of users for an org, you should be able to use:
https://api-d.docusign.net/management/v2/organizations/{OrgID}/users?URLParameters.
The URL parameters for this particular call require one of the following: account_id, organization_reserved_domain_id, or email. You're required to use at least one, but you should also be able to use more than one parameter at a time.
The result looks something like this:
{
"users": [
{
"id": "28fbe7e4-xxxx-xxxx-xxxx-c26569aa827c",
"user_name": "Matt_SATest_1",
"first_name": "Matt",
"last_name": "K",
"user_status": "active",
"email": "email#example.com",
"created_on": "2020-08-25T20:38:57.533"
}
],
"paging": {
"result_set_size": 1,
"result_set_start_position": 0,
"result_set_end_position": 0,
"total_set_size": 1
}
}

Resources