社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

ambianBeing

ambianBeing 最近创建的主题
ambianBeing 最近回复了
6 年前
回复了 ambianBeing 创建的主题 » 如何在Mongodb中将每个数组字段匹配到其他字段

如果 test 数组数据存储在一个集合中,那么实现O/P非常简单 $lookup 具有 $project 聚合

$arrayElemAt 为什么?因为查找将作为 testInform

db.maindocs.aggregate([
  {
    $lookup: {
      from: "testdocs",
      localField: "name",
      foreignField: "name",
      as: "testInform"
    }
  },
  {
    $project: {
      _id: 0,
      name: 1,
      company: 1,
      testInform: { $arrayElemAt: ["$testInform", 0] }
    }
  }
]);

其思想是迭代 cursor Array.prototype.find() 对象来自 符合 name 字段,将其添加到结果中。

const test = [
  {
    _id: 1,
    name: "apple",
    car: "ford"
  },
  {
    _id: 2,
    name: "melon",
    car: "ferrari"
  },
  {
    _id: 3,
    name: "perl",
    car: "Renaut"
  }
];


const cursor = db.collection("maindocs").find();
const result = [];

while (await cursor.hasNext()) {
  const doc = await cursor.next();
  const found = test.find(e => e.name === doc.name);
  if (found) {
    doc["testInform"] = found;
  }
  result.push(doc);
}

console.info("RESULT::", result);

如果使用 $regex 查询运算符不需要附加 / 因为那样的话 而不是正则表达式。 $正则表达式 将在求值时使其成为正则表达式。

  • 有趣的是你在用 GET 使用表单(可以搜索),确保 req.query 带着 query

只需将字符串传递给regex运算符:

/*GET params in express go in req.query only*/
const queryToSearch = req.query.query;
Product.find({title: {$regex: queryToSearch,'$options': 'i'}});
  • 一经要求,请拨打两次电话:

应该是 OPTIONS 请求通常称为 pre-flight request Read More