从 Mongodb 中的对象数组中查找对象

发布于 2025-01-11 09:29:13 字数 1353 浏览 0 评论 0原文

const Schema = new Schema({
    queueId: {
        type: String,
        required: true,
        index: {
            unique: true
        }
    },
    players: {
        type: [
            {
                ID: {
                    type: String,
                    required: true,
                    index: {
                        unique: true
                    },
                    default: 'null'
                },
                name: {
                    type: String,
                    required: true,
                    default: 'null',
                },
                queueId: {
                    type: String,
                    required: true,
                    default: 'null'
                }
            }
        ],
        required: true,
        default: []
    },
    isAvailable: {
        type: Boolean,
        required: true,
        default: true
    },
    isFull: {
        type: Boolean,
        required: true,
        default: false
    }
});

如何使用 findOne()players 获取对象,这是一个数组

我目前正在尝试此代码,但它

const doc = await List.findOne({ players: { ID: 'id' } });

基本上返回 null players 是一个数组,我想从对象的players数组中查找ID并获取文档。

const Schema = new Schema({
    queueId: {
        type: String,
        required: true,
        index: {
            unique: true
        }
    },
    players: {
        type: [
            {
                ID: {
                    type: String,
                    required: true,
                    index: {
                        unique: true
                    },
                    default: 'null'
                },
                name: {
                    type: String,
                    required: true,
                    default: 'null',
                },
                queueId: {
                    type: String,
                    required: true,
                    default: 'null'
                }
            }
        ],
        required: true,
        default: []
    },
    isAvailable: {
        type: Boolean,
        required: true,
        default: true
    },
    isFull: {
        type: Boolean,
        required: true,
        default: false
    }
});

How do I use findOne() to get the object from players which is an array

Im currently trying this code but it returns null

const doc = await List.findOne({ players: { ID: 'id' } });

basically players is an array and I want to find ID from the players array of objects and get the document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2025-01-18 09:29:13
db.collection.find({
  "players.ID": "1"
},
{
  "players.$": 1
})

mongoplayground


db.collection.find({
  "players.ID": "1"
})

mongoplayground


db.collection.aggregate([
  {
    $match: {
      "players.ID": "1"
    }
  },
  {
    $unwind: "$players"
  },
  {
    $match: {
      "players.ID": "1"
    }
  }
])

mongoplayground

db.collection.find({
  "players.ID": "1"
},
{
  "players.
quot;: 1
})

mongoplayground


db.collection.find({
  "players.ID": "1"
})

mongoplayground


db.collection.aggregate([
  {
    $match: {
      "players.ID": "1"
    }
  },
  {
    $unwind: "$players"
  },
  {
    $match: {
      "players.ID": "1"
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文