ozomer / node-red-contrib-mongodb2

MongoDB driver node for Node-RED
Apache License 2.0
15 stars 19 forks source link

MongoDB3 trouble with InsertMany #33

Closed germancmartinez closed 5 years ago

germancmartinez commented 5 years ago

Hi,

I´m having problems with the InsertMany method. I´m getting the error "MongoError: docs parameter must be an array of documents". I have tried putting the document array in the msg.payload and msg.payload.docs, but I'm always getting this error. Any help is welcome! Thanks!

jomel commented 5 years ago

when message payload is an array, each array element is interpreted as a parameter to the selected operation. eg when operation= "update", message.payload=[{surname:"smith"}, {"surname":"Smith"}, {"multi":true}]

since for you are wanting to supply an array as a single parameter, you should wrap it in a second array: message.payload=[[1,2,3,4]] and not message.payload=[1,2,3,4]

mrdelarosatorres commented 5 years ago

when message payload is an array, each array element is interpreted as a parameter to the selected operation. eg when operation= "update", message.payload=[{surname:"smith"}, {"surname":"Smith"}, {"multi":true}]

since for you are wanting to supply an array as a single parameter, you should wrap it in a second array: message.payload=[[1,2,3,4]] and not message.payload=[1,2,3,4]

Thanks!

cudochinigi commented 2 years ago

for insertMany ✗ msg.payload = [ { "name" : "Luffy" , "value" : 1} , { "name" : "Zoro" , "value" : 2 } ] ✓ msg.payload = [ [ { "name" : "Luffy" , "value" : 1} , { "name" : "Zoro" , "value" : 2 } ] ]

VINITARORA-21 commented 6 months ago

this is the correct format db.collectionname.insertMany([ {} , {} , {} ])

collectionname would be your collection u have created in ur database

{ } inside this your objects would be there eg:

try { db.products.insertMany( [ { _id: 13, item: "envelopes", qty: 60 }, { _id: 13, item: "stamps", qty: 110 }, { _id: 14, item: "packing tape", qty: 38 } ] ); } catch (e) { print (e); }

refer this

https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#mongodb-method-db.collection.insertMany