如何在 mongoDB 文档中查找和打印对象数组
How to find and print array of objects in mongoDB document
我在 mongoDB 中有此文档:
{
"_id": {
"$oid": "628f739398580cae9c21b44f"
},
"events": [
{
"eventName": "Dans",
"eventText": "Danse",
"eventDate": "010101"
},
{
"eventName": "Spill",
"eventText": "Spille",
"eventDate": "020202"
}
],
"school": "Høyskolen Kristiania"
}
我试图让每个事件(名称、文本和日期)都在它们自己的 div 中,但似乎无法单独访问每个“块”。它们应该作为一个打印出来,并且只在学校匹配的地方打印,我的目的是为每所学校制作不同的文件并从那里通过查询进行过滤。不过,这不是问题。我能够将它们全部作为一个对象数组或类似的
{
[dev:server] _id: new ObjectId("628f739398580cae9c21b44f"),
[dev:server] events: [ [Object], [Object] ],
[dev:server] school: 'Høyskolen Kristiania'
[dev:server] }
我的 API 目前是这样的:
当然,名称将由 userinfo 发送,为了测试目的而硬编码。
router.get("/", async (req, res) => {
const name = "Høyskolen Kristiania";
const schools = await mongoDatabase
.collection("tempschool")
.find()
.toArray();
console.log(schools);
res.json(schools);
});
还有我的客户:
function EventCard({ event }) {
const { eventName, eventDate, eventText } = event;
return (
<div>
<h1>{eventName}</h1>
<h3>{eventDate}</h3>
<div>{eventText}</div>
</div>
);
}
export function SchoolPage() {
const {loading, error, data} = useLoader(
async () => await fetchJSON("/api/schools")
);
const school = data;
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return (
<div>Error</div>
);
}
return (
<div>
{school.map((event) => (
<div key={event.name}>
<EventCard event={event}/>
</div>
))}
</div>
);
}
我不知道您是否将 tempschool
创建为 MongooseSchema
。不过,您应该将其查询为
const school = await TempSchool.findOne({school: "Høyskolen Kristiania"});
school.events
然后会给你一个对象数组。您将在 front-end 上使用它作为
return(
<div>
school.events.map((event) => (
<div>
<h1>{event.eventName}</h1>
<h3>{event.eventDate}</h3>
<div>{event.eventText}</div>
</div>
)
)
</div>
);
我在 mongoDB 中有此文档:
{
"_id": {
"$oid": "628f739398580cae9c21b44f"
},
"events": [
{
"eventName": "Dans",
"eventText": "Danse",
"eventDate": "010101"
},
{
"eventName": "Spill",
"eventText": "Spille",
"eventDate": "020202"
}
],
"school": "Høyskolen Kristiania"
}
我试图让每个事件(名称、文本和日期)都在它们自己的 div 中,但似乎无法单独访问每个“块”。它们应该作为一个打印出来,并且只在学校匹配的地方打印,我的目的是为每所学校制作不同的文件并从那里通过查询进行过滤。不过,这不是问题。我能够将它们全部作为一个对象数组或类似的
{
[dev:server] _id: new ObjectId("628f739398580cae9c21b44f"),
[dev:server] events: [ [Object], [Object] ],
[dev:server] school: 'Høyskolen Kristiania'
[dev:server] }
我的 API 目前是这样的: 当然,名称将由 userinfo 发送,为了测试目的而硬编码。
router.get("/", async (req, res) => {
const name = "Høyskolen Kristiania";
const schools = await mongoDatabase
.collection("tempschool")
.find()
.toArray();
console.log(schools);
res.json(schools);
});
还有我的客户:
function EventCard({ event }) {
const { eventName, eventDate, eventText } = event;
return (
<div>
<h1>{eventName}</h1>
<h3>{eventDate}</h3>
<div>{eventText}</div>
</div>
);
}
export function SchoolPage() {
const {loading, error, data} = useLoader(
async () => await fetchJSON("/api/schools")
);
const school = data;
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return (
<div>Error</div>
);
}
return (
<div>
{school.map((event) => (
<div key={event.name}>
<EventCard event={event}/>
</div>
))}
</div>
);
}
我不知道您是否将 tempschool
创建为 MongooseSchema
。不过,您应该将其查询为
const school = await TempSchool.findOne({school: "Høyskolen Kristiania"});
school.events
然后会给你一个对象数组。您将在 front-end 上使用它作为
return(
<div>
school.events.map((event) => (
<div>
<h1>{event.eventName}</h1>
<h3>{event.eventDate}</h3>
<div>{event.eventText}</div>
</div>
)
)
</div>
);