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

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

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

问题

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

  1. router.put("/delete/team/:id/:org", (req, res) => {
  2. const orgid = req.params.org;
  3. const _id = req.params.id;
  4. Organization.findOneAndUpdate(
  5. {
  6. _id: orgid,
  7. },
  8. {
  9. $pull: {
  10. teams: { _id: _id },
  11. },
  12. },
  13. { multi: true }
  14. )
  15. .then((organization) => {
  16. res.status(200).json(organization);
  17. })
  18. .catch((err) => {
  19. res.status(400).json(err);
  20. });
  21. });

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

  1. const OrganizationSchema = new Schema({
  2. owner: {
  3. type: mongoose.Schema.Types.ObjectId,
  4. ref: "User",
  5. },
  6. register_date: {
  7. type: Date,
  8. default: Date.now,
  9. },
  10. teams: [
  11. {
  12. sport: {
  13. type: String,
  14. required: false,
  15. },
  16. access_code: {
  17. type: Number,
  18. required: false,
  19. },
  20. admin: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
  21. events: [
  22. {
  23. date_time: {
  24. type: Date,
  25. offset: true,
  26. },
  27. opponent: {
  28. type: String,
  29. required: true,
  30. },
  31. home_away: {
  32. type: String,
  33. required: true,
  34. },
  35. expected_attendance: {
  36. type: Number,
  37. },
  38. people_attending: [
  39. { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  40. ],
  41. amenities: [String],
  42. },
  43. ],
  44. },
  45. ],
  46. });
英文:

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 -->

  1. router.put(&quot;/delete/team/:id/:org&quot;, (req, res) =&gt; {
  2. const orgid = req.params.org;
  3. const _id = req.params.id;
  4. Organization.findOneAndUpdate(
  5. {
  6. _id: orgid,
  7. },
  8. {
  9. $pull: {
  10. teams: { _id: _id },
  11. },
  12. },
  13. { multi: true }
  14. )
  15. .then((organization) =&gt; {
  16. res.status(200).json(organization);
  17. })
  18. .catch((err) =&gt; {
  19. res.status(400).json(err);
  20. });
  21. });

<!-- 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 -->

  1. const OrganizationSchema = new Schema({
  2. owner: {
  3. type: mongoose.Schema.Types.ObjectId,
  4. ref: &quot;User&quot;,
  5. },
  6. register_date: {
  7. type: Date,
  8. default: Date.now,
  9. },
  10. teams: [
  11. {
  12. sport: {
  13. type: String,
  14. required: false,
  15. },
  16. access_code: {
  17. type: Number,
  18. required: false,
  19. },
  20. admin: [{ type: mongoose.Schema.Types.ObjectId, ref: &quot;User&quot; }],
  21. events: [
  22. {
  23. date_time: {
  24. type: Date,
  25. offset: true,
  26. },
  27. opponent: {
  28. type: String,
  29. required: true,
  30. },
  31. home_away: {
  32. type: String,
  33. required: true,
  34. },
  35. expected_attendance: {
  36. type: Number,
  37. },
  38. people_attending: [
  39. { type: mongoose.Schema.Types.ObjectId, ref: &quot;User&quot; },
  40. ],
  41. amenities: [String],
  42. },
  43. ],
  44. },
  45. ],
  46. });

<!-- end snippet -->

答案1

得分: 0

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

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

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

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

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

  1. $pull: {
  2. teams: { _id: new ObjectID(_id) },
  3. },

这将帮助您正确匹配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,

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

And then,

  1. $pull: {
  2. teams: { _id: new ObjectID(_id) },
  3. },

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:

确定