0

I've struggling trying to make my mongoose query return an array, since I need to:

  1. Find 0..N docs from one collection;
  2. Save this found array in another nested doc collection;

My code:

CarIndex.find({ '_id': { $in: ids } }, 'carID carBrand carModel location')
            .then(collections => {
                const out = []

                collections.map( (doc) => {
                    const {carID ,carBrand ,carModel ,location} = doc
                    const car = {carID ,carBrand ,carModel ,location};
                    out.push(car)
                })

                console.log(out)
                console.log(out.length)
                console.log(typeof out)

                return CarCollection.findOneAndUpdate({ '_id': collectionId }, { $addToSet: { carCollection: { $each: { out } } } })
            });

The output error:

[04/01/2018 11:04:48.980] [LOG]

[ { carID: 'd82e41b0-f14f-11e7-b845-990cb852c2b3',
    carBrand: 'Peugeot',
    carModel: '207 Sed. Passion XR Sport 1.4 Flex 8V 4p',
    location: [-23.539727799999998,-46.5111749] },
  { carID: 'd82f2c10-f14f-11e7-b845-990cb852c2b3',
    carBrand: 'Nissan',
    carModel: 'Sentra 2.0/ 2.0 Flex Fuel 16V Mec.',
    location: [-23.607240972099525,-46.72912079051677] } ]

[04/01/2018 11:04:48.982] [LOG] 2

[04/01/2018 11:04:48.983] [LOG] object

[04/01/2018 11:04:48.997] [ERROR] MongoError: The argument to $each in $addToSet must be an array but it was of type object

4
  • But here console.log(out) is an array so what is the issue Commented Jan 4, 2018 at 13:17
  • Actually it is not, if you check the 3rd log, it shows object, leading to the error! Thanks Commented Jan 4, 2018 at 13:21
  • in javascript everything is Object array is also Object. you can check like this out instanceof Array Commented Jan 4, 2018 at 13:23
  • Hmm good catch, but why can't I save it using the $addToSet? Commented Jan 4, 2018 at 13:26

1 Answer 1

1

you can do like this

 CarIndex.find({ '_id': { $in: ids } }, 'carID carBrand carModel location')
                .then(collections => {
                    const out = []

                    collections.map( (doc) => {
                        const {carID ,carBrand ,carModel ,location} = doc
                        const car = {carID ,carBrand ,carModel ,location};
                        out.push(car)
                    })

                    console.log(out)
                    console.log(out.length)
                    console.log(typeof out)

                    return CarCollection.findOneAndUpdate({ '_id': collectionId }, { $addToSet: { carCollection: { $each: out  } } })
                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.