我想知道我做错了什么。
我使用 Sailsv0.10 和 mongo2.6.0 并希望通过 native 更新集合中的数组字段(使用 $push)。
我的模型:
module.exports = {
schema: true,
attributes: {
username: {
type: 'string',
required: true
},
pubs: {
type: 'array',
defaultsTo: []
},
...
我的功能:
User.native(function (err, collection) {
collection.update({username:aUsernameVariable},{$push:{pubs:aPubsVariable}}, function (err) {
});
到目前为止它有效。但是,为什么它不能用作带有 id 字段的查询?
User.native(function (err, collection) {
collection.update({id:anIdVariable},{$push:{pubs:aPubsVariable}}, function (err) {
});
我肯定会使用正确的查询 ID 来测试它。
我做错了什么?或者是Sails-Mongo适配器的ObjectId类型转换问题
最佳答案
如果您想使用 native(),您始终可以直接在您的 mongo-DB 中尝试相同的查询。因为 _id 是一个 Object-id 你应该
var ObjectId = require('mongodb').ObjectID;
User.native(function (err, collection) {
collection.update({_id: new ObjectId(anIdVariable)},{$push:{pubs:aPubsVariable}}, function (err) {
});
您可以使用 npm install mongodb --save
将 mongo-native-driver 添加到您的应用中
关于mongodb - Sails with Mongo 中的 native 更新不适用于 ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699097/