- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 mongoose 将项目添加到 ObjectId 数组中 - 我使用 $addtoset 因为我只想在项目尚不存在时添加该项目。
但是我的行为很奇怪。 $addtoset 正在将一个 objectId 附加到数组中,但它不是我传递给它的那个。
我已经从代码中删除了其他所有内容,以尝试缩小范围,现在该方法如下所示简单,但它仍在发生。
userSchema.statics.addPlaylistToUser = function(user, playlistId, done){
var toss = playlistId.toString(); //I verify at this point the playlistId is correct, eg. ObjectId("53cf855ac551004417e54da2")
User.findByIdAndUpdate(
user._id,
{$addToSet: {playlists: playlistId } },
{safe: true, upsert: false},
function(err, model) {
//at this point, an object Id has been added to the array, but it is not the same as verified above, the objectid added is eg. ObjectId("53cf8572c551004417e54da4")
done(err, model);
})
}
有趣的是,ObjectId 始终几乎相同,这表明新的 ID 是在我实际添加的 ID 之后立即生成的。上面的示例 objectid 是运行代码的实际示例。
此方法是在回调中从实际生成我要添加的 ObjectId 的方法中调用的...我的问题可能与此相关吗?
任何关于为什么会发生这种情况的帮助/想法将不胜感激。
我找到了this similar question就这样,但他们的解决方案似乎非常适合他们的情况,我无法从那里的答案中得到任何想法......
最佳答案
根据 API 文档,选项字段中的属性“new”必须为 true 才能返回修改后的文档。否则,默认返回原始文档。
http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate
关于node.js - Mongoose:为什么 $addtoset 添加不正确的 ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24907999/
我一直在尝试使用 node.js api 在 findAndModify 查询中使用 $addToSet。但是我收到一条错误消息,指出前缀字段 $addToSet 对存储无效。查询可以使用 mongo
我在数据库记录的实体中有一个对象数组。我想将新对象推送到该数组,但我不想多次推送同一个对象。我知道我可以使用$addToSet,但是问题出现在我添加时间戳的每个对象中,所以这个方法不区分对象已经在数组
我在一个文档中有一个(mongodb)嵌入文档列表,我有兴趣将一个新的嵌入文档添加到现有文档列表中。 据我研究,我可以使用 $addToSet,我不明白的是 MongoDB 如何决定新文档是否已存在于
$addToSet 好像只能添加到数组,是否可以将散列添加到散列? { "a"=>"1", "b"=>"2", "c"=>{"d"=>"3"} } 到 { "a"=>"
鉴于此示例文档: > db.sample.find().pretty() { "_id" : ObjectId("570f76ca4fe66c8ae29f13cd"), "a" : [
所以我希望我的收藏中的文档在结构上看起来像这样: { "_id": "123", "systems": [ { "_id": "1338",
当使用 $addToSet 时,如果我运行两次,它会输入重复的对象,但 name 和 key 的顺序相反。无论如何要避免这种情况,这样它只会增加一次? var categories = [{ na
我在 MongoDB 中更新文档时遇到问题,涉及使用 Pymongo 添加到列表和更新某些字段。 总而言之,我想: 向列表添加一个值。 更新一些字段。 使用单个更新语句。 我尝试了两种方法,但都不起作
http://www.mongodb.org/display/DOCS/Updating#Updating-%24addToSet 虽然文档中没有具体说明,但我推测此操作将始终将元素(如果唯一)添加到
如果字段不存在,使用 $addToSet 的数组更新是否会创建该字段?换句话说,如果下面一行中的 field2 不存在,是否会通过此命令创建: db.collection.update({id: },
假设我有一个文档,其中包含一个名为 Months 的数组。数组中的每个元素都有一个 int 字段,指示它引用的月份和年份 (yyyyMM),例如201612、201701等 每个此类元素还包含与该特定
我正在尝试更新嵌套在另一个散列下的数组对象。即 { name: "mike", instagram: { id: 3423, slug: 'mike', photos: [] } } 现在我想使用 $
我的基地里有一些文件: //example docs {"_id": "qwerty12345", "name": "Bob", "cards":["cardId1", "cardId2", "car
是否可以$addToSet并确定哪些项目已添加到集合中? 即$addToSet 标记到帖子并返回实际添加的内容 最佳答案 事实并非如此,而且没有任何一句话。您可以获得的最接近的是 findAndMod
我有以下更新查询: var where = 'answers.round_' + ans.round + '_' + ans.iteration Game.findByIdAndUpdate(game
我有一个userSchema,其中包含一个 id 和一个 friend 数组。 friend 数组实际上是另一个模式的数组,其中包含 id、性别和姓名,因此是一个 SubDocuments 数组。 v
我正在尝试对来自 mongoDB 集合(使用 nodeJS 驱动程序)的多个字段中的数组中的唯一值进行排序。 一个小数据集: [{ "_id" : "5c93db3dd018451640601
我需要通过使用变量来使用字段名称。我该怎么做? let variableName = 'cars'; Collection.update( { _id: id }, {
我有一个像下面这样的文档: { _id: "00112233", array: [ { _id: "potatoes",
我有以下文件: { "recordKey": "FOO", "channels": [{ "id": "CH1", "blocks": [] }, { "id": "CH2", "blo
我是一名优秀的程序员,十分优秀!