在表示 ObjectID 的字符串数组中查找猫鼬 ObjectID 的问题
Issue finding mongoose ObjectID in array of strings representing ObjectIDs
我需要在这样的数组中找到 mongoose objectID 的索引:
[ { _id: 58676b0a27b3782b92066ab6, score: 0 },
{ _id: 58676aca27b3782b92066ab4, score: 3 },
{ _id: 58676aef27b3782b92066ab5, score: 0 }]
我用来比较的模型是具有以下数据的猫鼬模式:
{_id: 5868d41d27b3782b92066ac5,
updatedAt: 2017-01-01T21:38:30.070Z,
createdAt: 2017-01-01T10:04:13.413Z,
recurrence: 'once only',
end: 2017-01-02T00:00:00.000Z,
title: 'Go to bed without fuss / coming down',
_user: 58676aca27b3782b92066ab4,
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false }
我正在尝试使用以下方法在上面的数组中找到 model._user 的索引:
var isIndex = individualScores.map(function(is) {return is._id; }).indexOf(taskList[i]._user);
其中individualScores为原始数组,taskList[i]为任务模型。然而,这总是returns-1。它永远不会在数组中找到正确的 _id。
我猜你的问题与你的查询_id
如何返回有关
如果你得到 _id
作为 String
,你的代码应该可以工作,检查下面的片段
但如果相反,您得到 ObjectsId
s,则必须先将它们转换为字符串
var individualScores = [
{ _id: "58676b0a27b3782b92066ab6", score: 0 },
{ _id: "58676aca27b3782b92066ab4", score: 3 },
{ _id: "58676aef27b3782b92066ab5", score: 0 }
]
var task = {
_id: "5868d41d27b3782b92066ac5",
updatedAt: new Date("2017-01-01T21:38:30.070Z"),
createdAt: new Date("2017-01-01T10:04:13.413Z"),
recurrence: 'once only',
end: new Date("2017-01-02T00:00:00.000Z"),
title: 'Go to bed without fuss / coming down',
_user: "58676aca27b3782b92066ab4",
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false
}
var isIndex = individualScores.map(
function(is) {
return is._id;
})
.indexOf(task._user);
console.log(isIndex)
我认为你的流程应该运行良好,你正在使用只需要将 'objectID' 转换为 String
进行比较。 _id
和 _user
.
均使用 .toString()
进行转换
像下面这样:
var isIndex = individualScores.map(function(is) {
return is._id.toString();
}).indexOf(taskList[i]._user.toString());
我需要在这样的数组中找到 mongoose objectID 的索引:
[ { _id: 58676b0a27b3782b92066ab6, score: 0 },
{ _id: 58676aca27b3782b92066ab4, score: 3 },
{ _id: 58676aef27b3782b92066ab5, score: 0 }]
我用来比较的模型是具有以下数据的猫鼬模式:
{_id: 5868d41d27b3782b92066ac5,
updatedAt: 2017-01-01T21:38:30.070Z,
createdAt: 2017-01-01T10:04:13.413Z,
recurrence: 'once only',
end: 2017-01-02T00:00:00.000Z,
title: 'Go to bed without fuss / coming down',
_user: 58676aca27b3782b92066ab4,
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false }
我正在尝试使用以下方法在上面的数组中找到 model._user 的索引:
var isIndex = individualScores.map(function(is) {return is._id; }).indexOf(taskList[i]._user);
其中individualScores为原始数组,taskList[i]为任务模型。然而,这总是returns-1。它永远不会在数组中找到正确的 _id。
我猜你的问题与你的查询_id
如何返回有关
如果你得到 _id
作为 String
,你的代码应该可以工作,检查下面的片段
但如果相反,您得到 ObjectsId
s,则必须先将它们转换为字符串
var individualScores = [
{ _id: "58676b0a27b3782b92066ab6", score: 0 },
{ _id: "58676aca27b3782b92066ab4", score: 3 },
{ _id: "58676aef27b3782b92066ab5", score: 0 }
]
var task = {
_id: "5868d41d27b3782b92066ac5",
updatedAt: new Date("2017-01-01T21:38:30.070Z"),
createdAt: new Date("2017-01-01T10:04:13.413Z"),
recurrence: 'once only',
end: new Date("2017-01-02T00:00:00.000Z"),
title: 'Go to bed without fuss / coming down',
_user: "58676aca27b3782b92066ab4",
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false
}
var isIndex = individualScores.map(
function(is) {
return is._id;
})
.indexOf(task._user);
console.log(isIndex)
我认为你的流程应该运行良好,你正在使用只需要将 'objectID' 转换为 String
进行比较。 _id
和 _user
.
.toString()
进行转换
像下面这样:
var isIndex = individualScores.map(function(is) {
return is._id.toString();
}).indexOf(taskList[i]._user.toString());