0

In back end I'm using mongoose, express and node js. I want to do joins in mongodb. If this is SQL it's not hard. Since I'm new to mongo database. This is problem to me. I already tried many answers in this platform and none of them help my situation.

I have following two schema in my database.

Item

import mongoose from 'mongoose';
const Schema = mongoose.Schema;
export const item_schema = new Schema({
    item_no: {
        type: String
    },
    name: {
        type: String
    },
    item_category: {
        type: String
    },
    no_of_copies: {
        type: Number
    },
    public_or_rare: {
        type: String
    },
    user_no: {
        type: String
    }

});

Book

import mongoose from 'mongoose';

const Schema = mongoose.Schema;

export const book_schema = new Schema({
    item_no: {
        type: String
    },
    isbn_no: {
        type: String
    },
    author: {
        type: String
    },
    published_year: {
        type: Number
    },
    publisher: {
        type: String
    },
    book_category: {
        type: String
    }
});

Note that there is no error in these models because those runs well with other controllers in my API

I want to get book and item tables data as one object where book and item share same item_no

In order to achieve my aim so far I tried following code,

export const get_books_all_details = (req, res) => {
     Book.find({}, (err, book) => {
        if (err) {
            res.send(err);
        }
        else {
            let my_book = book;
           // res.json(my_book);
            Item.findOne({ item_no: my_book.item_no }, (err, item) => {
                //Item.find({}, (err, item) => {
                if (err) {
                    res.send(err);
                }
                else {
                    let my_item = item;
                    //res.send(my_book+my_item);
                    res.json({
                        /* item_no: my_item.item_no,
                        item_name: my_item.item_name,
                        item_category: my_item.item_category,
                        no_of_copies: my_item.no_of_copies,
                        public_or_rare: my_item.public_or_rare,
                        isbn_no: my_book.isbn_no,
                        author: my_book.author,
                        published_year: my_book.published_year,
                        publisher: my_book.publisher,
                        book_category: my_book.book_category , */
                        book:my_book,
                        item:my_item
                    })
                }
            });
        }
    }); 
};

In top I use find() to get all books and if there is record in item table which matches item_no I want to get that particular item too. That's why I used findOne() inside find() I don't know is it right or wrong !

So this new guy wait for you guys help and really appreciate it. If you could help me with code and explanations that will be great !

sashiksu
  • 354
  • 4
  • 15

1 Answers1

0

I want to do joins in mongodb

Just don't. MongoDB is a NoSQL database, specifically built to avoid joins and to enforce denormalization of data. If you need to join a document, put it inside of another document. If you build your collections in MongoDB like you do in any SQL database, you'd just have a non ACID compliant relational database, which is dangerous.

Vladyslav Usenko
  • 2,296
  • 8
  • 18