英文:
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);
});
});
每个Organization
在OrganizationSchema
中都有一个唯一的_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("/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);
});
});
<!-- 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: "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],
},
],
},
],
});
<!-- 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('mongodb').ObjectID
And then,
$pull: {
teams: { _id: new ObjectID(_id) },
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论