MongoDB:如何从对象数组中提取匹配ObjectID的整个对象?

huangapple go评论86阅读模式
英文:

MongoDB: how can I pull an entire object from an object array that matches the Objectid?

问题

我的问题是,如何在teams中拉取一个与团队的唯一_id匹配的团队对象(在teams数组中)。
以下是我尝试过的方法,但问题是所有在teams中的条目都被删除,而不仅仅是与团队_id匹配的对象。

router.put("/delete/team/:id/:org", (req, res) => {
  const orgid = req.params.org;
  const _id = req.params.id;

  Organization.findOneAndUpdate(
    {
      _id: orgid,
    },
    {
      $pull: {
        teams: { _id: _id },
      },
    },
    { multi: true }
  )
    .then((organization) => {
      res.status(200).json(organization);
    })
    .catch((err) => {
      res.status(400).json(err);
    });
});

每个OrganizationOrganizationSchema中都有一个唯一的_id
而且,teams数组中的每个团队(对象)都有一个唯一的_id

const OrganizationSchema = new Schema({
  owner: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  register_date: {
    type: Date,
    default: Date.now,
  },
  teams: [
    {
      sport: {
        type: String,
        required: false,
      },
      access_code: {
        type: Number,
        required: false,
      },
      admin: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
      events: [
        {
          date_time: {
            type: Date,
            offset: true,
          },
          opponent: {
            type: String,
            required: true,
          },
          home_away: {
            type: String,
            required: true,
          },
          expected_attendance: {
            type: Number,
          },
          people_attending: [
            { type: mongoose.Schema.Types.ObjectId, ref: "User" },
          ],
          amenities: [String],
        },
      ],
    },
  ],
});
英文:

My question is, how I can pull a team object in teams that matches the unique _id of a team(in teams array).
Here is what I have tried, however, the problem is that all entries in teams get deleted, instead of only the object that matches the team _id.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

router.put(&quot;/delete/team/:id/:org&quot;, (req, res) =&gt; {
  const orgid = req.params.org;
  const _id = req.params.id;

  Organization.findOneAndUpdate(
    {
      _id: orgid,
    },
    {
      $pull: {
        teams: { _id: _id },
      },
    },
    { multi: true }
  )
    .then((organization) =&gt; {
      res.status(200).json(organization);
    })
    .catch((err) =&gt; {
      res.status(400).json(err);
    });
});

<!-- end snippet -->

Each Organization in OrganizationSchema has a unique _id.
Also, each team(object) in teams array has a unique _id.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const OrganizationSchema = new Schema({
  owner: {
    type: mongoose.Schema.Types.ObjectId,
    ref: &quot;User&quot;,
  },
  register_date: {
    type: Date,
    default: Date.now,
  },
  teams: [
    {
      sport: {
        type: String,
        required: false,
      },
      access_code: {
        type: Number,
        required: false,
      },
      admin: [{ type: mongoose.Schema.Types.ObjectId, ref: &quot;User&quot; }],
      events: [
        {
          date_time: {
            type: Date,
            offset: true,
          },
          opponent: {
            type: String,
            required: true,
          },
          home_away: {
            type: String,
            required: true,
          },
          expected_attendance: {
            type: Number,
          },
          people_attending: [
            { type: mongoose.Schema.Types.ObjectId, ref: &quot;User&quot; },
          ],
          amenities: [String],
        },
      ],
    },
  ],
});

<!-- end snippet -->

答案1

得分: 0

因为您正在将ObjectId与字符串进行匹配,导致您得到的输出可能的原因是这样。

您需要将字符串转换为对象。

您可以通过添加以下代码来实现:

const ObjectID = require('mongodb').ObjectID;

然后,可以使用以下方式进行匹配:

$pull: {
    teams: { _id: new ObjectID(_id) },
},

这将帮助您正确匹配ObjectId。

英文:

The probable reason for the output you are getting is, because you are matching an ObjectId with a string.

You need to convert your string to an object.

You can do this by adding,

const ObjectID = require(&#39;mongodb&#39;).ObjectID

And then,

 $pull: {
        teams: { _id: new ObjectID(_id) },
      },

huangapple
  • 本文由 发表于 2023年1月4日 08:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/74999820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定