Aggregate Join in MongoDB not returning results - node.js

I am trying to do an aggregate join to find patient information in the 'patients' table based on 'patient_id' field in the bookings table. Aggregate join is not returning results even though there are matching records in the 'patients' table. Am I doing something wrong?

If the _id field in the "patients" collection is an ObjectId, it won't match when you compare to the string value stored in patient_id. You can use the $addFields aggregation step to add a new field to the documents with the "patient_id" field cast to an ObjectId, but I'd advise you to instead store reference id's as ObjectIds

Related

Filter, Sort and Paginate in mongoose returns duplicate value

I have a product document in mongodb. I am retrieving data with paging using skip and limit after filtering and sorting, like this using mongoose -
Product.find({gender:'male'})
.sort(price: 1)
.skip(page * 25).limit(25);
When page value is 0 it returns the result as expected, but when the page value changes to 1 it returns some results that were already retrieved when page value was 0. I am passing all these parameters through query in nodjs express. How can i retrieve unique value only with sorting and filtering while using pagination with mongoose ?
As mentioned here:
MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order.
If consistent sort order is desired, include at least one field in
your sort that contains unique values. The easiest way to guarantee
this is to include the _id field in your sort query.
So you can solve the problem by:
Product.find({gender:'male'})
.sort({price: 1, _id: 1})
.skip(page * 25).limit(25);

Search collection fields and populated fields at the same time

I have 2 collections as the following:
Products
- name (String)
- price (number)
- category (ObjectID refs to Category collection)
Category
- name
And we have search query for example "some query"
I'm trying to get the records where product.name or product.category.name partially includes the search query
If you are using Mongoose populate, that uses DBRef, which is not easily used on the server side.
If the Products document contains only the _id of the corresponding Category document, you would need to use aggregation to read all Products documents, and for each one load all of the corresponding Category documents, then filter using a regular expression.
That is not scalable.
If you denormalize a little bit by having the Products document contain both the _id and name of the Category (assuming Category actually has other data), you could use a text index on the Products collection, or if you are using Atlas, full text search in the Products collection.

Mongoose/ MongoDB: Get _ids from names

Given an array of strings and a collection with that string and primary key in MongoDB, how can I get the _id's of those strings if exists and insert a record if not present in the collection?
The collection has only name and its _id in it.

Search query on fieldvalue varchar ('SUM-123') in elasticsearch

I am trying to search for a document in elasticsearch.
Field name is sum and Field value is 'SUM-123'
I created a bool query to matchQuery('sum','SUM-123').
But it is not returning the exact sum field documents instead it is returning the documents with different field values.
Thanks.
If you want to do an exact string matching, you should not analyzed your index.
More info here

Comparing and Sorting MongoDB ObjectIDs in Node - convert to string?

I'm trying to compare, sort, filter, etc. arrays of MongoDB ObjectIDs as well as sort documents based on an arrays. My main question is: should I use ObjectID.equals() or is it okay to convert them all to strings, use native comparisons like indexOf, and convert them back? Does it matter?
My specific use case: a user can save posts to a list. I save this as an array of ObjectIDs user.saves. I want to retrieve the first 25 posts in order, so I query {_id: {$in: user.saves.slice(0, 25)}}. How would you sort retrieved documents?
solution:
db.collection.find({
$or: [{
_id: _id1
}, {
_id: _id2
}]
}, callback)
The purpose of the ObjectId in Mongodb is to uniquely identify a document.
You should never need use it for sorting operations, certainly by not converting to a string.
If all you want to do is get a list of documents sorted by the time they were created.
You should add created_at (timestamp) field, you can use this to sort the documents when you need to.
If you don't want to add the extra field. The ObjectId contains timestamp component, so you could extract this and use that for sorting. But the common way is to add the extra field.

Resources