Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One to many relationship mongoose. #5989

Closed
0san0 opened this issue Jan 12, 2018 · 3 comments
Closed

One to many relationship mongoose. #5989

0san0 opened this issue Jan 12, 2018 · 3 comments
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@0san0
Copy link

0san0 commented Jan 12, 2018

mongodb version v3.6.0
npm version 5.6.0
node version v8.9.3

I have used two schema blog and its comment.where it has a unidirectional relationship between blogs and comments (1 to Many).

const BlogPostSchema = mongoose.Schema({
  Title: { type: String },
  Description: { type: String },
  Image: { type: String }
});

const CommentSchema = mongoose.Schema({
  // object reference from BlogPost
  BlogPost  : { type: mongoose.Schema.Types.ObjectId, ref: 'BlogPost' },
  Image: { type: String }
  Message: { type: String }
});

var comment = Comment.find().populate('BlogPost').exec();

Using this query, I am able to fetch all comments with its blog.

[
    {
        "_id": "5a45d53f4c02c537427a52a7",
        "Message": "This is my test first comment.",
        "BlogPost": {
            "_id": "5a45d4f35193c636b3c1b673",
            "Description": "This is blog description.",
            "Title": "This is my blog."
            "Image": "36a3d5e0.jpg",
        },
       "Image": "66a3d5e0.jpg",
        "__v": 0
    },
    {
        "_id": "5a45d5794c02c537427a52a8",
        "Message": "This is my test second comment.",
        "BlogPost": {
            "_id": "5a45d4f35193c636b3c1b673",
            "Description": "This is blog description.",
            "Title": "This is my blog."
            "Image": "36a3a5e0.jpg",
        },
        "Image": "66a3d5e1.jpg",
        "__v": 0
    },
]

which is working fine.
1.How would I fetch all blogs with its comment with this unidirectional (reverse) one to many relationships?
2.How would I change "Image": "66a3d2f1.jpg" to "Image": "https://www.example.com/uploads/66a3d2f1.jpg" value?
Expected result.

[
    {
        "_id": "5a45d4f35193c636b3c1b673",
        "Description": "This is blog description.",
        "Title": "This is my blog.",
        "Image": "66a3d2f1.jpg",
        "__v": 0,
        "Comments": [
            {
                "_id": "5a45d53f4c02c537427a52a7",
                "Message": "This is my test first comment.",
                "BlogPost": "5a45d4f35193c636b3c1b673",
                "Image": "66a3d5q1.jpg",
                "__v": 0
            },
            {
                "_id": "5a45d5794c02c537427a52a8",
                "Message": "This is my test second comment.",
                "BlogPost": "5a45d4f35193c636b3c1b673",
                "Image": "66a3d5f3.jpg",
                "__v": 0
            },
            {
                "_id": "5a45ebe562509738a12c3eb5",
                "Message": "This is my test second comment.",
                "BlogPost": "5a45d4f35193c636b3c1b673",
                "Image": "66a3d5f1.jpg",
                "__v": 0
            }
        ]
    },
    {
        "_id": "5a45d5c54c02c537427a52a9",
        "Description": "This is blog description.",
        "Title": "This is my blog.",
        "Image": "66a3e5f1.jpg",
        "__v": 0,
        "Comments": [
            {
                "_id": "5a45ebec62509738a12c3eb6",
                "Message": "This is my test second comment.",
                "BlogPost": "5a45d5c54c02c537427a52a9",
                "Image": "66a3d5e0.jpg",
                "__v": 0
            },
            {
                "_id": "5a4d05dcfe3557278310e6ac",
                "Message": "This is my test second comment.",
                "BlogPost": "5a45d5c54c02c537427a52a9",
                "Image": "66a3d5e1.jpg",
                "__v": 0
            }
        ]
    }
]
@0san0 0san0 changed the title Modify mongoose json result. One to many relationship mongoose. Jan 12, 2018
@sobafuchs
Copy link
Contributor

  1. you can use a virtual populate: read this blog post for more info on how to use this API: http://thecodebarbarian.com/mongoose-virtual-populate

  2. I'm not sure I understand the question. Can't you just query for it in the database based on that Image property and update it?

@sobafuchs sobafuchs added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Jan 15, 2018
@0san0
Copy link
Author

0san0 commented Jan 18, 2018

This is the response, I have got.

[
    {
        "_id": "5a45d4f35193c636b3c1b673",
        "Description": "This is blog description.",
        "Title": "This is my blog.",
        "Image": "66a3d2f1.jpg",
        "__v": 0,
        "Comments": [
            {
                "_id": "5a45d53f4c02c537427a52a7",
                "Message": "This is my test first comment.",
                "BlogPost": "5a45d4f35193c636b3c1b673",
                "Image": "66a3d5q1.jpg",
                "__v": 0
            }
        ]
    }
]

I mean how would i modify json response that i am getting through populating model?
But the expected response is below.

[
    {
        "_id": "5a45d4f35193c636b3c1b673",
        "Description": "This is blog description.",
        "Title": "This is my blog.",
        "Image": "https://www.example.com/uploads/66a3d2f1.jpg",
        "__v": 0,
        "Comments": [
            {
                "_id": "5a45d53f4c02c537427a52a7",
                "Message": "This is my test first comment.",
                "BlogPost": "5a45d4f35193c636b3c1b673",
                "Image": "https://www.example.com/uploads/66a3d5q1.jpg",
                "__v": 0
            }
        ]
    }
]

@sobafuchs
Copy link
Contributor

I'm sorry I still don't understand. What do you want to change in the response / why can't you do it with javascript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants