Javascript 如何检查给定的输入值是否可用?在对象中

Javascript 如何检查给定的输入值是否可用?在对象中,javascript,mongodb-query,Javascript,Mongodb Query,下面是我从控制台.log(对象)中得到的信息: 我有一个输入数组,例如:让uniqueid=[“0”、“1”、“10”]。我必须检查gallary.profilepic给定的输入值是否可用?假设不可用意味着我必须推送一个数组并返回结果 我的代码: let uniqueIDs = ["0","1","10"] db.Gallary.find() .forEach(function(objects){ console.log(objects); // Here your JS cod

下面是我从控制台.log(对象)中得到的信息:

我有一个输入数组,例如:
让uniqueid=[“0”、“1”、“10”]
。我必须检查gallary.profilepic给定的输入值是否可用?假设不可用意味着我必须推送一个数组并返回结果

我的代码:

let uniqueIDs = ["0","1","10"]
db.Gallary.find()
.forEach(function(objects){
    console.log(objects);
    // Here your JS code has to come
})
预期产出:

["0", "10"]

过滤您的
upid
数组,检查其中哪些项未包含在
成员的
常规学生
道具中
数据:

let-upid=[“0”、“1”、“10”];
常数数据=[{
“_id”:“5bb20d7556db6915846da55f”,
“成员”:{
“常客”:[
"1",
"2"
]
}
},
{
“_id”:“5bb20d7556db6915846da55f”,
“成员”:{
“常客”:[
"3",
"4"
]
}
}];
const regularStudents=data.map(d=>d.members.regularStudent.flat();

console.log(UPIDs.filter(a=>!regularlstudents.includes(a)))您可以收集所有ID的数组(我在我的示例中使用过),然后将UPID中未出现在该数组中的所有元素取出

const data=[{u id:“5bb20d7556db6915846da55f”,“成员”:{“正规学生”:[“1”,“2”]},{u id:“5bb20d7556db6915846da55f”,“成员”:{“正规学生”:[“3”,“4”]};
常量UPIDs=[“0”、“1”、“10”];
//将数据中的ID连接在一起
const id=data.reduce((acc,c)=>acc.concat(c.members.regularlstudent),[]);
//`filter`out upid中不在ids数组中的元素
const out=UPIDs.filter(el=>!ids.includes(el));

控制台。注销我将首先获取数据库中与您想要输入的数据相匹配的所有值(否则它将加载您的所有集合条目),然后查看缺少的字符串。如有必要,最后推送一个新条目

const UPIDs = ['0','1','10'];

const entries = await db.Groups.find({
  'members.regularStudent': {
     $in: UPIDs,
  },
});

// Get the strings that are not in the DB
const missing = UPIDs.filter(x => !entries.some(y => y.members.regularStudent.includes(x)));

// Push the missing str in DB
if (missing.length) {
  const obj = new model({
     members: {
        regularStudent: missing,
     },  
  });

  // ...
}


constupids=['0','1','10'];
//发送至@Andy的Thx
const entries=[{“_id”:“5bb20d7556db6915846da55f”,“成员”:{“正规学生”:['1',2']}}];
/*
const entries=wait db.Groups.find({
“会员。常客”:{
$in:upid,
},
});
*/
//获取不在数据库中的字符串
const missing=UPIDs.filter(x=>!entries.some(y=>y.members.regularStudent.includes(x));
//将缺少的str按入DB
如果(缺少长度){
console.log('为值添加新数据',缺失);

}
不要使用块引号进行样式设置。只有在引用某个东西时才使用块引号。你能再详细一点吗?我不明白UPID和学生名单之间的关系。请通读,尤其是你最好的选择是做你的研究,相关的主题,并尝试一下。如果你在做了更多的研究和搜索后陷入困境,无法摆脱困境,请发布一份你的尝试,并明确指出你陷入困境的地方。人们会很乐意提供帮助。你说不可用是什么意思?@Oscar Calderon,UPIDs等于
regularStudent
valuesHow我可以获得
数据
值吗,因为你使用的是
数据
数组,但我得到的是对象我在Snippet中添加了一个示例,因为我无法在stackoverflow中执行mongodb请求。。。该代码段可以作为示例使用。无论您在stackoverflow中看到什么,它都不会被复制/粘贴。您必须了解正在发生的事情,并根据实际情况调整它
对象
——就像在
控制台中一样。log(objects)
——是我假设的对象数组,否则您将无法获得该输出。
const UPIDs = ['0','1','10'];

const entries = await db.Groups.find({
  'members.regularStudent': {
     $in: UPIDs,
  },
});

// Get the strings that are not in the DB
const missing = UPIDs.filter(x => !entries.some(y => y.members.regularStudent.includes(x)));

// Push the missing str in DB
if (missing.length) {
  const obj = new model({
     members: {
        regularStudent: missing,
     },  
  });

  // ...
}