【问题标题】:How to check given input values available or not ? in objects如何检查给定的输入值是否可用?在对象中
【发布时间】:2019-01-09 12:27:24
【问题描述】:

这是我从console.log(objects) 得到的信息:

{
    "_id" : "5bb20d7556db6915846da55f",
    "gallary" : {
        "profilepic" : [
            "1",
            "2"
        ]

    }
},
{
    "_id" : "5bb20d7556db6915846da55f",
    "gallary" : {
        "profilepic" : [
            "3",
            "4"
        ]

    }
}

我有一个输入数组,例如:let uniqueIDs = ["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 和学生列表之间的关系。
  • 请通读help center,尤其是How do I ask a good question?,你最好的办法是做你的研究,search关于SO的相关主题,然后试一试。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到困难的地方。人们会很乐意提供帮助。
  • 不可用是什么意思?
  • @Oscar Calderon,UPID 等于 regularStudent

标签: javascript mongodb-query


【解决方案1】:

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

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,
     },  
  });

  // ...
}


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

// Thx to @Andy
const entries = [{"_id":"5bb20d7556db6915846da55f","members":{"regularStudent":['1', '2']}}];

/*
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) {
  console.log('Add new data for values', missing);
}

【讨论】:

  • 我在 sn-p 中添加了一个示例
  • 因为我无法在stackoverflow中执行mongodb请求...以sn-p为例。无论您在 stackoverflow 中看到什么,它都不会被复制/粘贴。您必须了解正在发生的事情并根据您的实际情况进行调整
【解决方案2】:

过滤您的 UPIDs 数组,检查其中哪些项目未包含在您的 members 数据的 regularStudent 属性中:

let UPIDs = ["0","1","10"];
const data = [{
    "_id" : "5bb20d7556db6915846da55f",
    "members" : {
        "regularStudent" : [
            "1",
            "2"
        ]

    }
},
{
    "_id" : "5bb20d7556db6915846da55f",
    "members" : {
        "regularStudent" : [
            "3",
            "4"
        ]

    }
}];

const regularStudents = data.map(d => d.members.regularStudent).flat();
console.log(UPIDs.filter(a => !regularStudents.includes(a)));

【讨论】:

  • 我如何获取data 值,因为您使用的是data 数组,但我正在获取对象
【解决方案3】:

您可以将所有 ID 集合在一起(我在示例中使用了 reduce),然后 filter 将 UPID 中未出现在该数组中的所有元素收集起来。

const data = [{"_id":"5bb20d7556db6915846da55f","members":{"regularStudent":["1","2"]}},{"_id":"5bb20d7556db6915846da55f","members":{"regularStudent":["3","4"]}}];
const UPIDs = ["0","1","10"];

// Concatenate the ids in the data together
const ids = data.reduce((acc, c) => acc.concat(c.members.regularStudent), []);

// `filter` out the elements in UPIDs that aren't in the ids array
const out = UPIDs.filter(el => !ids.includes(el));

console.log(out);

更新说明:

这是你的代码和我的代码:

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

db.Groups.find().forEach(function(objects){
  const ids = objects.reduce((acc, c) => acc.concat(c.members.regularStudent), []);
  const out = UPIDs.filter(el => !ids.includes(el));  
  console.log(out);
});

【讨论】:

  • objects - 如console.log(objects) - 是我假设的对象数组,否则您将无法获得该输出。
猜你喜欢
  • 2021-07-26
  • 1970-01-01
  • 2014-12-03
  • 2020-04-18
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多