4

I have this "find()" result querying all the data from my 'someTable'.

someTable

[ { "_id": "5e029b3ca7e71e06464c4c35", "someitem": "somedata1", "user": { "_id": "5de18423bddb7c058f97d57b" }, "hobbie": { "_id": "5dec6b377c1e970d8627d8a5" } }, { "_id": "5e039f3fe8d47e18fe940e33", "someitem": "somedata2", "user": { "_id": "5e039ea5e8d47e18fe940e32" }, "hobbie": { "_id": "5dec6bc07c1e970d8627d8a8" } }, { "_id": "5e039f6ae8d47e18fe940e34", "someitem": "somedata3", "user": { "_id": "5e039ea5e8d47e18fe940e32" }, "hobbie": { "_id": "5dec6bc07c1e970d8627d8a8" } } ] 

So, I need to get all someitemS filtered by two fields: hobbie._id and user._id, I was researching over the internet but I can not find any clue about this.

someTable.js (node class with express)

router.get("/my-some-table/:hobbieId", async (req, res, next) => { const userId = "5e039ea5e8d47e18fe940e32"; const hobbieId = req.params.hobbieId; // get table data by hobbieId and userId const someRegisters = await Some.find( { user._id: userId, //<--this 'user._id' is generating a sintax error hobbie._id: knowId //<--this 'hobbie._id' is generating a sintax error } ); res.send(someRegisters); }); 

MySchema

const someSchema = new mongoose.Schema({ someitem: { type: String, required: true, }, user: { type: mongoose.Schema.Types.ObjectId, ref: "User" }, hobbie: { type: mongoose.Schema.Types.ObjectId, ref: "Hobbie" } }); 

How can I refer to a referenced document id?

1 Answer 1

2

Use the below query, go inside user Object then search, you can also search by global _id then you can show your user data.

example Query:

db.YourColl.find({ "user": { _id: ...}, "hobbie": { _id: ... }} ); 

Solved Your Problem below, Query:

db.yourColl.find({ "user": { _id: "5e039ea5e8d47e18fe940e32"}, "hobbie": { _id: "5dec6bc07c1e970d8627d8a8"}} ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.