Mongo dart 在列表中插入元素
Mongo dart insert element in list
我正在尝试在 mongoDB 列表中插入一个元素。我正在使用 flutter 和包 mongo_dart
.
这是我正在执行的呼叫:
await usersCollection.modernUpdate(
where.eq('_id', userService.currentUser.id),
modify.addToSet('favorites', favorite)
);
问题是,如果我传递一个字符串而不是 favorite
,它就可以工作。但是,如果我传递一个像 favorite 这样的自定义对象,我将检索此错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Exception: Not implemented for Instance of 'Favorite'
#0 new BsonObject.bsonObjectFrom
package:bson/src/bson_type.dart:157
#1 BsonObject.elementSize
package:bson/src/bson_type.dart:206
#2 BsonMap.dataSize.<anonymous closure>
package:bson/…/types/map.dart:27
#3 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#4 BsonMap.dataSize
package:bson/…/types/map.dart:26
#5 BsonMap.byteLength
package:bson/…/types/map.dart:36
#6 BsonObject.elementSize
package:bson/src/bson_type.dart:206
#7 BsonMap.dataSize.<anonymous closure>
package:bson/…/types/map.dart:27
#8 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
我不明白,因为 favorite
是实例化的。
我找到了解决方案。
只需将数据作为 json 传递即可。
await usersCollection.modernUpdate(
where.eq('_id', userService.currentUser.id),
modify.push('favorites', {"id": id, "data": DateTime.now()})
);
我正在尝试在 mongoDB 列表中插入一个元素。我正在使用 flutter 和包 mongo_dart
.
这是我正在执行的呼叫:
await usersCollection.modernUpdate(
where.eq('_id', userService.currentUser.id),
modify.addToSet('favorites', favorite)
);
问题是,如果我传递一个字符串而不是 favorite
,它就可以工作。但是,如果我传递一个像 favorite 这样的自定义对象,我将检索此错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Exception: Not implemented for Instance of 'Favorite'
#0 new BsonObject.bsonObjectFrom
package:bson/src/bson_type.dart:157
#1 BsonObject.elementSize
package:bson/src/bson_type.dart:206
#2 BsonMap.dataSize.<anonymous closure>
package:bson/…/types/map.dart:27
#3 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#4 BsonMap.dataSize
package:bson/…/types/map.dart:26
#5 BsonMap.byteLength
package:bson/…/types/map.dart:36
#6 BsonObject.elementSize
package:bson/src/bson_type.dart:206
#7 BsonMap.dataSize.<anonymous closure>
package:bson/…/types/map.dart:27
#8 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
我不明白,因为 favorite
是实例化的。
我找到了解决方案。
只需将数据作为 json 传递即可。
await usersCollection.modernUpdate(
where.eq('_id', userService.currentUser.id),
modify.push('favorites', {"id": id, "data": DateTime.now()})
);