一个ObjectId数组中的Mongoose indexOf [英] Mongoose indexOf in an ObjectId array

查看:142
本文介绍了一个ObjectId数组中的Mongoose indexOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下架构:

var daySchema = new Schema({
date: {
    type: Date,
    required: true
},
activities: [{
    type: Schema.Types.ObjectId,
    ref: 'Activity'
}]
});

我正在删除白天的活动架构:

And I am removing an activity within the daySchema:

var index = day.activities.indexOf("584aa9c16791eb1ec4ad8e73");
day.activities.splice(index, 1);

有人可以解释我为什么这样做吗?。数组活动是 ObjectId的数组。因此, indexOf不应与对象一起使用,但 indexOf仍能够基于id查找元素。这让我发疯,还有其他事情要做,也许是ObjectId中的map函数?

Can someone explain me why this works ?. The array "activities" is an array of "ObjectId". So the "indexOf" should not work with objects, but still the "indexOf" is able to find the element based on the id. This is driving me crazy, is something else going on here, maybe a map function inside ObjectId ?

推荐答案

这是有效的,因为猫鼬包装了 MongooseArray 中的一个数组,它提供了自己的 indexOf 方法支持基于字符串的比较,而不是本机数组实现所使用的严格相等性测试。

This works because Mongoose wraps an array in MongooseArray which provides its own indexOf method which supports this string-based comparison rather than the strict equality test used by the native array implementation.

这篇关于一个ObjectId数组中的Mongoose indexOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆