My test
collection seems like this:
{
_id: ...,
owner: '...',
mapList: [
{
type: '...',
expireAt: '...',
isUsed: ...,
password: '...',
},
{
type: '...',
expireAt: '...',
isUsed: ...,
password: '...',
}
]
}
I want to
- check the mapList in the specific
owner
contains the map that is equal tomapList.type
, and is not expired withmapList.expireAt
, and is not used withmapList.isUsed
. - set a new map in a
mapList
. This new map will replace the old map that has the sametype
. - set the
type
inmapList
withunique
(don't havetype
duplicate inmapList
)
If the mapList
is an object
instead of an array
these can be done in one step with this
final WriteResult writeResult = await mongodb.testDbCollection.updateOne(
where.eq('owner', '...').eq('mapList.type', '...').lt('mapList.expireAt', '...').eq('mapList.isUsed', false),
modify.set('mapList.expireAt', '...').set('mapList.isUsed', '...').set('mapList.password', '...'));
return writeResult.isSuccess && writeResult.nModified == 1;
How can do something like this with array (List<Map<String,dynamic>>)
?
I have tried this (I tried updateOne
and got an error so I tried findOne
)
await mongodb.testDbCollection.findOne(where.eq('owner', '...').eq('mapList.type', 'type 1').eq('mapList.isUsed', false));
with these data:
{
_id: ...,
owner: '...',
mapList: [
{
type: 'type 1',
expireAt: '...',
isUsed: true,
password: '...',
},
{
type: 'type 2',
expireAt: '...',
isUsed: false,
password: '...',
}
]
}
And I got one result!
The MongoDB joins the type
field at index 0 with the isUsed
field at index 1.
Or I must fecth by owner
first then check it by my self?