0

Im working on a NodeJS RESTful API, using mongoose and MongoDB of course.

I have a mongoose model like this

const mySchema = mongoose.Schema({
  _id: {type: mongoose.Schema.Types.ObjectId},
  Cities: [{type: mongoose.Schema.Types.ObjectId, ref: 'City'}]

}

and a route like this

router.post('/', (req, res, next) => {
    for (const cityId of req.body.cities) {
        City.findById(cityId)
            .then(city => {
                if (!city) {
                   return routingResult.NotFound(res, "One city doesnt exists.");
                }
             })
             .catch(err => {
                 console.error(err);
                 routingResult.Error(res, err);
             });

As you can see, i'm trying to check if ALL of the elements on req.body.cities array exists on Database before i create a new document and save it.

I need to know if there's a way to check all elements at the same time. (those elements are ObjectID's that references another collection on my DB), something like City.find({"$all":req.body.cities}) ... i know this is not the proper implementation, i just want to know if there's something like that.

If any of the objectId's doesn't exists i must not create the new document to be inserted on Cities collection.

Is there a way to achieve this ?

May
  • 1
  • Not sure what you mean here. The `.findById()` methods are really only useful for actually matching an `_id` value you already know is there. What are you actually sending in the request? A list of "names" maybe? Do the items in the "cities" collection actually have another unique property such as the name? – Neil Lunn May 03 '18 at 03:40
  • Hi. No... look... cities array will have only ObjectId's "Cities: [0: objectId1, 1: ObjectId2, 2: ObjectId3]" and i need to check if those ObjectId's actually EXISTS on their respective Collection on my DB... don't know if i'm being clear... dont' really care if i need to use findbyid or find or some other function – May May 03 '18 at 03:49
  • i need something like Schema.find({"_id": {"$all": req.body.cities}}).exec().then().catch(); ..... i know this is not the way of do it... but i need something like that – May May 03 '18 at 03:52
  • It's not clear really. you might want `$in` but since we have no idea what you actually have in `req.body.cities` or what your true intention is then we can only guess at what you mean. You should include all relevant content within your question's here as well as explaining "why" you think you need to do this. Because when you actually explain why, then we can probably see a better way. – Neil Lunn May 03 '18 at 03:55
  • ok... different approach.... lets think in a shopping cart... You have Products and you have orders... an order contains a list of products, but before you create your order, you need to check if those products still exists on DB (maybe some of them were deleted because store doesnt sell them anymore, and that happened just when you were doing your order) .... is this more clear? ... i need to check if ALL of those products exists on DB before i create the order... – May May 03 '18 at 04:10
  • @neil Lunn ... hi..... i think this is not a duplicate question... i'm not asking how to enforce foreign keys.... i just need to know how to verify existence of every element in an array (Collection 1) checking it against other object (Collection 2) ... in my case i'm using ObjectId's, but it can be any other kind of data actually – May May 03 '18 at 15:09

0 Answers0