1

the artist collection consists of the detail of the artist and the art collection consist of the detail of the art by the particular artist. I want to merge both of them . I'm new to mongodb,in SQL I know how to perform the operation, but finding it difficult with mongo

Artist collection

{
    "_id" : ObjectId("571113f39542ab860bce85d7"),
    "name" : "ajay",
    "email" : "[email protected]",
    "password" : "d6124b9d34be470dd0387dff9170c825cf8934ed",
    "country" : "in",
    "address" : "mysore",
    "date_of_birth" : {
        "year" : "1992:05:03"
    },
    "type" : "artist"
}

Art Collection

{
    "_id" : ObjectId("57161d2307e2432a49082903"),
    "type" : null,
    "originalname" : null,
    "user_id" : "571113f39542ab860bce85d7",
    "fullPath" : "/code/wd/artifu/backend/uploads/568eec81d2051abe2996f7139d46d143.png",
    "created" : ISODate("2016-04-19T11:57:23.731Z"),
    "data" : {
        "title" : "image",
        "description" : "description",
        "tags" : "forest"
    }
}
2
  • Mongo does not support joins, you could use DBRef instead Commented Apr 20, 2016 at 5:43
  • You might also wanna check out the populate function of Mongoose, if you are allowed to use it. Commented Apr 20, 2016 at 5:47

1 Answer 1

1

after going through the documents I was able to merge it, thanks for thoes who commented :)

function new(req, res, next) {
    db.arts.findOne({
        _id: mongoskin.helper.toObjectID(req.params._id)
    }, function(err, art) {
        if (err) return next(err);
        if (!art) {
            return res.status(404).send({
                status: '404 file not found'
            });
        }
        db.users.findOne({
            _id: mongoskin.helper.toObjectID(art.user_id)
        }, function(err, user) {
            if (err) return next(err);
            if (!user) {
                return res.status(404).send({
                    status: '404 User not found'
                });
            }
            return res.send({
                user,art
            });

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

Comments

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.