These are chat archives for Automattic/mongoose

18th
May 2018
Ehsan Shadmehri
@ehsansh
May 18 2018 07:02

Hi. How can I find an id exist in an array of mongoose object ids? I have a mongoose schema with a field that contains array of mongoose object ids I have defined it like this:

    selected:[
        mongoose.Schema.Types.ObjectId
    ]

The problem is that:

selected.includes(req.params.id)

Always returns false I used typeof and found that req.params.id is a string and selected contains objects. I try to use JSON.parse() But nothing changes.
The only way that I could find is a for loop But is there any better way? How could I use includes() function?

Ehsan Shadmehri
@ehsansh
May 18 2018 07:19
selected.includes(mongoose.Types.ObjectId(req.params.id))
return false too.
Kev
@lineus
May 18 2018 07:38
@ehsansh there's a method on mongoose arrays called .id() that lets you find the doc in the array by either the id or the _id property. example
oh, they're not subdocs, just the _ids
sorry I'm just waking up
if index in the example is -1, then that id doesn't exist in the array
Kim Kiragu
@Kimkykie
May 18 2018 07:59
//--8/+=-h4 br6 lu8io-p[\=4=-0987654321k.7//
/--/+
Ehsan Shadmehri
@ehsansh
May 18 2018 09:09
@lineus Thanks a lot.
Kev
@lineus
May 18 2018 09:36
anytime :smile:
Sedric Heidarizarei
@saeedhei
May 18 2018 09:51
Hellow, How Can I backup my database or Collections with a node.js Script?
Kev
@lineus
May 18 2018 09:56
@saeedhei what do you mean by backup? what exactly do you want the script to do?
Sedric Heidarizarei
@saeedhei
May 18 2018 10:28
For Example I run my app in local windows for 2 days and I get 200 user ID, after That I want to backup my db and put that in other pc or mlab, How Should I do that with node.js code ?
or is there any sync script to sync my db?
Kev
@lineus
May 18 2018 15:26
@saeedhei Using the combination of mongodump and mongorestore seems like a reasonable solution from the mongodb docs. Scripting these shouldn't be terribly difficult using node.
Sedric Heidarizarei
@saeedhei
May 18 2018 15:35
@lineus thanks, today I will check that👍
warlockD
@warlockdn
May 18 2018 19:08
hey guys . I am querying this when I receive a result all works fine. but when no result found then I am not receiving any errors as well
return await Customer.findOne(query, 'customerID name phone email password', (err, customer) => {
        if (err) {
            logger.error('Error finding customer', query);
            return {
                status: 'ERROR'
            };
...
basically if no results are found then if (err) is null