1

I have two ids: publisherID and BookID.

My mongoose database looks like:enter image description here

Now the question is: I want to find userId where the publishderId is 59l55llopd3W1FuosGgN and the bookId is G1Vfe2NwS3sy-qhT1J0g6

I've created the following function which returns the null data.

const allTheRelatedData = (publiserId, bookId) => {
    return new Promise((resolve, reject) => {
        try {
            myBookSchema
                .findOne({
                    publisherId: publisherId,
                    "books.$.bookId": bookId,
                })
                .then((data) => {
                    console.log(data);
                    resolve(data);
                })
                .catch((error) => {
                    console.log(error);
                    reject(error);
                });
        } catch (error) {
            console.log(error);
            reject(error);
        }
    });
};

When I run this code, it gives null data. Please help, I tried many solutions such as positional, match, etc. Nothing is working. Please help.

2 Answers 2

4

You can use $ projection operator to do that:

myBookSchema
  .findOne({
    publisherId: publisherId,
    "books.bookId": bookId,
  }, {
    "books.$": 1
  }
).then((data) => {...

Then you can get userId with data.books[0].userId and remember to check if data or book exists with if (data && data.books && data.books.length)...

Sign up to request clarification or add additional context in comments.

9 Comments

Can you please tell what is 1 in the line: books.$: 1? As I don't know the index of the book.
It will select the first book that match "books.bookId": bookId in the condition. You can find more info in the link I provided
Sorry, but this code: .findOne( { publisherId: publisherId, "books.bookId": bookId, }, { "books.$": 1 } ) is also not working.
And when I replaced 1 with 0 it gave me error: MongoError: positional projection cannot be used with exclusion
And "books.$": 1 is for projection, not for condition.
|
1

Maybe this would work

const allTheRelatedData = (publiserId, bookId) => {
    return new Promise((resolve, reject) => {
        try {
            myBookSchema
                .find()
                .then((data) => {
                    const item = data.filter(i => i.publisherId === publisherId && i.books.includes(book => book.bookId === bookId))
                    if (item) {
                        console.log(item);
                        resolve(item);
                    } else {
                        console.log("Not found");
                        reject("Not found");
                })
                .catch((error) => {
                    console.log(error);
                    reject(error);
                });
        } catch (error) {
            console.log(error);
            reject(error);
        }
    });
};

1 Comment

This one is not working @im2rando also getting a message data.filter is not a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.