Reputation: 1308
So, I'm trying to create a document from the elements of an array but it constantly throws Document Validation Error
.
The schema is like this:
userId: String,
terms: [{
value: String,
system: String
}],
group: String
I have an array of values
for terms
, ['hello', 'hey', 'hi']
Now, I want to be able to create
a document using the values
from the array like this:
{
userId: 'foo',
terms: [{value: 'hello'}, {value: 'hey'}, {value: 'hi'}],
group: 'bar'
}
The way I'm trying to do it but failing:
let arr = ['hello', 'hey', 'hi'];
Document.create({userId: 'somevalue', terms: {value: {$in: arr}}}) // validation error
Upvotes: 1
Views: 53
Reputation: 151112
Use .map()
let arr = ['hello', 'hey', 'hi'];
Document.create({
userId: 'somevalue',
terms: arr.map(value => ({ value }) )
})
That will actually transform the array content as:
[{value: 'hello'}, {value: 'hey'}, {value: 'hi'}]
and use that value as you create the document.
Upvotes: 2