如何迭代对象的猫鼬子文档数组

时间:2019-05-02 17:02:14

标签: mongodb mongoose subdocument

尝试实现依赖于对象的子文档数组的条件语句,因此我需要遍历数据库中的用户集合,并使用findIndex像JavaScript一样检查对象的每个用户子文档数组内部

用户集合

const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
  username: {
    type: String,
    unique: true,
    required: true,
    lowercase: true
  }
  friends: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    }
  ],
  family: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    }
  ],
  acquaintances: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    }
  ],
  following: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    }
  ],
  pendingFriendConfirmationData:[
    {
      storedUserId : {type: String},
      choosenCategories: [{label: {type: String}, value: {type: String}}]
    }
  ]
});

const Users = mongoose.model("Users", userSchema);
module.exports = Users;

现在我可以使用

访问“用户”集合
db.Users.find()

我的示例结果

let filter = {"_id": userId}
let projection = {username: 1, friends: 1, family: 1, acquaintances: 1, following: 1, pendingFriendConfirmationData: 1}

db.Users.findOne(filter, projection, (err, user)=>{
      console.log(user)
    })

{
   friends: [],
   family: [],
   acquaintances: [],
   following: [],
   _id: 5ca1a43ac5298f8139b1528c,
   username: 'ahmedyounes',
   pendingFriendConfirmationData: [
     {
       choosenCategories: [Array],
       _id: 5ccb0fcf81a7944faf819883,
       storedUserId: '5cc95d674384e302c9b446e8'
     }
   ]
 }

关注未决的FriendConfirmationData 以下来自MongoDB Compass的屏幕截图

enter image description here

我要这样迭代

let filter = {"_id": userId}
let projection = {username: 1, friends: 1, family: 1, acquaintances: 1, following: 1, pendingFriendConfirmationData: 1}

db.Users.findOne(filter, projection, (err, user)=>{
      let data = user.pendingFriendConfirmationData
      for(let i in data){
       if(data[i].choosenCategories.findIndex(v => v.label === "friend") !== -1){
        console.log("he is a friend")
      }
      }
    })

如何遍历未决的FriendConfirmationData和选择类别 如上

现在,如果我按照以下方式操作console.log(data)

db.Users.findOne(filter, projection, (err, user)=>{
      let data = user.pendingFriendConfirmationData
      console.log(data)
    })

我知道

enter image description here

1 个答案:

答案 0 :(得分:0)

我知道了Faster Mongoose Queries With Lean

lean选项告诉Mongoose跳过对结果文档进行水化处理。这样可以使查询更快并且占用更少的内存,但是结果文档是纯JavaScript对象(PO​​JO),而不是Mongoose文档。在本教程中,您将了解有关使用lean()的权衡的更多信息。

在我之前的示例中,解决方案将添加{lean:true}

db.Users.findOne(filter, projection, {lean: true}, (err, user)=>{
      let data = user.pendingFriendConfirmationData
      console.log(data)
    })

也在这里

db.Users.findOne(filter, projection, {lean: true}, (err, user)=>{
      let data = user.pendingFriendConfirmationData
      for(let i in data){
       if(data[i].choosenCategories.findIndex(v => v.value === "friends") !== -1){
        console.log("he is a friend")
      }
      }
    })

// he is a friend

结论

要遍历对象的深层嵌套子文档数组,请确保 您正在使用lean()处理纯JavaScript对象(PO​​JO)

db.Users.find().lean()