0

I am trying to relationships todo and user, my model is something like this

user =  new Schema({
  email: {
    type: String,
    unique: true,
    required: [true, "must has email"]
  },
  password: {
    type: String,
    required: [true, "fill your password"]
  },
  name: {
    type: String,
    required: [true, "must fill your name"]
  },
  task: [{ type: Schema.Types.ObjectId, ref: "Todo" }]



todo = owned_id: { type: Schema.Types.ObjectId, ref: "User" },
  task: {
    type: String,
    required: [true, "please fill your taks"]
  },
  done: { type: Boolean, default: false }

I create the task and get the owned_id by req.session.user which I save on session when the user login, it something like this, user add task then I fill the owned_id: req.session.user ( because I save their Object_id on session) then I tried to get the populate of the Todo user login, something like this

User.find({_id: req.session.user}).populate('task')

it doesn't't show anything,

and I tried on todo collection again, something like this,

Todo.find({owned_id: req.session.id}).populate('task')

it only show the user id , and the task is empty array

I really want to get the result of user login tasks, am I wrong to creat collections ?? or wrong on to use populate?

when I create task, I just put the owned_id that I got from req.session.user, the task on user, still empty, should I update the task it by the newTask_id when user create todo task ?

here is my data for todo collection

_id:5ca9ae3cd4198bdb3d68e93e
done:false
owned_id:5ca9a000932528c98fcc89cb
task:"doing something"
__v:0

and here is the user which create the task

_id:5ca9a000932528c98fcc89cb
task:Array
email:"dave@email.com"
password:"12345678"
name:"David Lee"
__v:0
poppop
  • 137
  • 13
  • Show the documents from both collections you expect to "join". These need to share the **same** `ObjectId` values in order for `populate()` or any other type of join to actually work. If you get no result then the most likely cause is the two values are not in fact the same. – Neil Lunn Apr 07 '19 at 08:09
  • what di u think about my schema? is that correct?? – poppop Apr 07 '19 at 08:12
  • The "schema" just says how "it's supposed to look". The actual important thing is the stored documents in the collection. You don't show anything in your question. You were pointed to the same existing answers before because they are showing you "common data" which is used for a join. You didn't provide that either now or then. You can show documents in your question if you think you have a different problem, but the most likely cause is that `tasks` and the `_id` in the foreign collection contain unrelated values. Or the incorrect type. – Neil Lunn Apr 07 '19 at 08:15
  • did u mean, u want to see full collection data I create ?? – poppop Apr 07 '19 at 08:23
  • I really get confused now, and I really get stuck, the duplicate u references is for mongoDB queries, I really want to get some help for the my populate, I have been work ion this all day :( – poppop Apr 07 '19 at 08:26
  • What I am saying is any question that says **this code does not work** needs to provide a **reproducible case**. We need "at least" two documents in order to **reproduce** a join ourselves do we not? So this is what is missing from your question. I see schema, and code, but it's missing **data**. Please also read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Neil Lunn Apr 07 '19 at 08:27
  • I added the data collection – poppop Apr 07 '19 at 08:33
  • Please show the documents as viewed from the `mongo` shell. Showing `console.log()` output does not show the full data ( not the `task: Array` ) nor does it show the actual BSON types assigned. You're also doing it the wrong way around. From the `ToDo` model it would be `Todo.find({owned_id: req.session.id}).populate('owned_id')` in order to get the `User`. But for the `User` way around it all depends on the data visible in that array and it's actual type. – Neil Lunn Apr 07 '19 at 08:38
  • when I create the todo task, I added $addToSet , and I show the populate now , but I am not sure if I correct :( – poppop Apr 07 '19 at 08:45

0 Answers0