Reputation: 271
I'm trying to upsert multiple documents at once by using a PUT verb to a loopback based Rest API. The body of the message contains an array of json objects.
[
{"_id" : "1",
"data" : "foo"
},
{"_id" : "2",
"data" : "bar"
}
]
On an empty database this works just fine (create). All documents are created as expected. But if I run the same call again (update), I receive an error containing an array of similar error messages:
E11000 duplicate key error index: testdatabase.node.$id dup key: { : "1" }
After some further investigation, I found out that if I pass a single object, the upsert works fine.
{"_id" : "1",
"data" : "foo"
}
BUT: if I pas an array with the same single object, the error is back.
[
{"_id" : "1",
"data" : "foo"
}
]
Single upserts are not an option, because I have to update thousands of documents using the Rest Api.
loopback version: 2.22
Upvotes: 0
Views: 890
Reputation: 1135
The upsert
function takes a single model as a paramater.
Take a look at the documentation.
In order to update different models as you want, you will have to use the native driver.
EDIT
I dug into loopback source code and found out that the PUT
request uses the upsert function of the connector.
Which means that it expects one model and not an array, maybe loopback doesn't enforce it.
You can find the relevant code here.
Anyway, the closest thing to upserting multiple models is the bulk operation of mongodb, but this leads to self implementation.
Upvotes: 0