These are chat archives for Automattic/mongoose

20th
Jan 2018
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:00
{
    "_id" : ObjectId("5a4cbb745cdf850ea8b8f124"),
    "title" : "Cricket Feast India vs Sri Lanka",
    "description" : "Test Description Sports",
    "eventType" : "sports",
    "sportEvent" : {
        "sportName" : "Cricket",
        "firstTeam" : "India",
        "secondTeam" : "Sri Lanka"
    }
}
this is one of the event with eventType: "sports"
Kev
@lineus
Jan 20 2018 00:03
i'm new at all of this too :) I hadn't ever used discriminators before I tested your example a bit.
have you tried using the SportEvent instead of the Event to do the find and update?
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:05
few of my friends told me the same thing. Any idea how can I accomplish this without using discriminator
Kev
@lineus
Jan 20 2018 00:08

instead of:

Event.findOneAndUpdate......

maybe try:

sportEvent.findOneAndUpdate.....
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:09
sportEvent is just one type of event. I have multiple eventType so it wont be practical
if eventType is expo a different schema is used instead of sportEvent
Kev
@lineus
Jan 20 2018 00:12
yeah, that makes sense. I'm not sure, let me play with it a bit and see if I can make sense of it.
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:12
const expoEvent = Event.discriminator("expo", new Schema({
  expoEvent: [{
    brandName: {
      type: String,
      required: true
    },
    brandInfo: {
      type: String,
      required: true
    }
  }]
}));
{
    "_id" : ObjectId("5a4cbdad5cdf850ea8b8f12c"),
    "title" : "First Test Expo",
    "description" : "Test Description Expo",
    "eventType" : "expo",
    "expoEvent" : [ 
        {
            "brandName" : "Nikon",
            "brandInfo" : "Nikon Cameras Nikon Cameras Nikon Cameras Nikon Cameras "
        }, 
        {
            "brandName" : "Brother Solutions",
            "brandInfo" : "Printers Printers Printers Printers Printers Printers Printers  "
        }
    ]
}
Kev
@lineus
Jan 20 2018 00:15
and the values in req.body that you're passing into the mongoose set command look ok?
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:15
yes
enough time has passed to check everything on my end
its been 7-8 days
I am on 5th page of google search
Kev
@lineus
Jan 20 2018 00:25
using sportevent instead of event makes it work for me.
I get that's not going to work for you, but it's the only way I can update the sportevent so far.
Kev
@lineus
Jan 20 2018 00:30
which kind of makes sense because the discriminator is the one that's aware of the extended fields.
maybe someone else can jump in and provide some insight :)
honestly, until today I hadn't even noticed discriminators in the docs, so I'm certainly not an authority. I can intuitively see why I think it doesn't work, but at the same time, I can see why you want it to work that way.
maybe sprinkle a little logic into your route that checks for eventType and calls the right discriminator find for that type?
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:35
haha
thank you so much for your time
@lineus
Yeah i may have to do it that way
Kev
@lineus
Jan 20 2018 00:36
happy to learn with you @rawalyogendra :)
Yogendra Rawal
@rawalyogendra
Jan 20 2018 00:36
but I thought I may have missed something because honestly it shouldn't be that way
Irfan Raza
@irfaan_aa_twitter
Jan 20 2018 21:20
Mongodb has option of picking documents randomly using $sample
How to achieve the same using mongoose?
Kev
@lineus
Jan 20 2018 21:24
The API docs mention aggregates here seems like it might be what you're looking for.
Irfan Raza
@irfaan_aa_twitter
Jan 20 2018 21:26
Thanks @lineus yes this is what I am looking for but I was searching it on doc 4.0. Seems I need to update to mongoose 5 to get this work
let me know if you have any knowledge if it exists on 4.x.x of mongoose
I don't want to update my production environment to 5 at this point
Kev
@lineus
Jan 20 2018 21:29
looks like the 4.x docs are here looks like its your lucky day :)
Irfan Raza
@irfaan_aa_twitter
Jan 20 2018 21:30
Awesome. Thanks :)