Get array of objects based on specific object Id - node.js

my Mongodb structure
id:111
article:Array
0:Object
articleid:"123"
1:Object
articleid:"456"
id:222
article:Array
0:Object
articleid:"789"
I want to get users based on articleid. If one user based on articleid is getting fine. But i have few articleid's, based on this articleid's i want to get users.
I tried like this
Collections.user.find({},{article:{$elemMatch:{articleid:req.body.Articleid}}})
but not working, when i check
console.log(req.body.Articleid)
["456", "789"]
I need to get users based on this articleid's.

Correct your query to:
Collections.user.find({'article.articleid':req.body.Articleid}}})
Explanation: The first argument is the query, second one is just for projection.

Related

Changing collection name according to query parameters in Nestjs

Can we get data from different collections of same or multiple mongo databases according to the query parameters in nest js ?
For example if parameter says get data from collection A, then collection A data should be displayed if it says get data from collection B, then collection B data should be displayed.
Can we do it in same controller or we need to make multiple controllers ?
I got it, I just have to make two models from different collections and use the desired model according to query parameter by using simple if then else.

Mongoose find and check if all queries are given

I have a problem with my app.
Im trying to set filters working, so im using mongoose find method.
I have something like that:
Campsite
.find({
name: req.query.name,
country: req.query.country
})
How i can force my query to check if user gave all the data?
For example i want user to search only by name and get results, search only by country and get results and search by both queries and also get result by combining both of values
Right now the code above works like that:
When user types name and leaves empty country it wont find anything because it kinda sends an empty country even if its disabled as an input and when i remove country property from my query and user search for the name he gets correct results.
How i can fix that?

Trying to do a where filter in findOne and in Node api returns empty array when filtering by ids in loopback

Trying to do a where filter in findOne and in Node api returns empty array when filtering by ids in loopback
https://url/api/Model1/findOne?filter={"where":{"attrs":"id"}}
where id is exactly 24 digits long
It is possible that you simple don't have an object with that ID in that particular collection in the database. In such a case you should get an empty array.
Or you may need to find by id and not by attr - it depends on how the relevant field in your database is named.
See the docs, there are good examples there: https://loopback.io/doc/en/lb2/Where-filter.html
E.g. this:
http://localhost:3000/api/Books?filter={"where":{"or":[{"id":1},{"id":2}]}}
Note that the id is used and not attr. See what is your field in your case.

Manipulating ref'd mongo records based on _id field

Ok so I have a pretty simple DB setup in a MEAN app (node, mongoose, mongo) where I have Book records, and User records. A book has a single Owner, and can have any number of shared users which are stored in an array in a field called sharedWith:. Originally I was storing the user records with an email address as the _id field. I now realize this was a dumb move on my part because if someone wants to change their email address it effectively cuts them off from their books.
The app is not live yet, so it's not a fatal mistake.
My question is, once I revert the User documents to using the original hash value for _id, and store those in the Owner and sharedWith fields in the book documents, will I have to query each hash just to retrieve the actual usable user data?
I know mongoose has a .populate() method which will resolve sub documents, but as for inserting them? Will I POST the users as email addresses, then query each and store the resulting hashes? I can do this manually, but I wanted to make sure there is not some secret mongo-sauce that can do this in the DB itself.
Thanks!
If you have the _id available in the frontend for the user. You can directly share him a book by adding the _id to the sharedWith array of a book. But if you don't have the _id of the user available in the frontend, you need to manually get the _id by querying with the email and then store the _id in the sharedWith. As to retrieve the books, populate is indeed the best option to use to get user data.
And to get all books shared with a user you can do something like this,
Book.find({sharedWith:user1._id},function(err,docs){ });
This query can be made efficient if you use an index on sharedWith but that depends on your use case.

Mongoose: Running a second query on the returned documents

Problem:
I have a 'Group' collection. Each group has an embedded document of 'Members'. I need to pull out a specific Member by their 'MemberID' and fetch all their details from the 'Users' collection. I would like to do this using the '.populate()' method but only need to populate that single member's record not all the members records.
So my query looks like this:
DB.model('groups')
.findById(groupID)
.populate('members._user')
.run(function(err, group){
// then loop over every member and return the one that matches
// the member id we require
});
This seems like a very inefficient way of doing things considering I only need the user details for one member in the group! I only have the memberID not the userID so this is the reason I am going to the members collection.
How can I extract a single member from the embedded 'members' document and populate it?
The populate function takes 3 parameters: path, fields and conditions. fields and conditions are applied when the referred document(s) is(are) populated via a separate call to model.find(...). Try passing to populate a valid mongodb condition that will only return members that you are interested in.

Resources