gpt4 book ai didi

node.js - 将帖子发送到 mongoose 时数组的 QUITE _id

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:50 24 4
gpt4 key购买 nike

我需要帮助来使用此模型创建数据库。(对于 api mongoose 和express,我可以使用 mongoshell 或 mongoose 模式)。

{
"hotels" : [{
"name" : "Hotel Emperador",
"stars" : "3",
"images" :[....],
"price" : "1596",
},
...]
}

但是当我将数组发布到 MongoDB 时,会自动为我的数组创建 _id:"" ,我可以删除它吗?我需要 _id 但对于数组内的文档,我得到如下内容:

{"_id": "5e181fed9fc1883a69797e3a", 
"hotels":[
{
"name" : "Hotel Emperador",
"stars" : 4,
"price" : 1596,
"imagen" : "https://i.ibb.co.jpg",
"id": 1
},...]}

我需要这样的东西,因为这对我来说非常困难。找到包含前面代码的文档。

{
"hoteles": [
{
"_id": "5e1217b81c9d440000632fd7",
"nombre": "Hotel Sonesta",
"direccion": "Cerritos",
"telefono": "3152020",
"estrellas": "5",
"precio": "850000",
"imagenes": "https://i.ibb.co/t2Hc8c4/Hotel-Sonesta.jpg"
},
{
"_id": "5e1218211c9d440000632fd8",
"nombre": "Hotel Soratama",
"direccion": "Centro",
"telefono": "3204545",
"estrellas": "4",
"precio": "540000",
"imagenes": "https://i.ibb.co/vcyqQSf/Soratama.jpg"
},..]}

我知道模型架构可能会有帮助,但尝试过但没有成功你能帮我吗?

最佳答案

你的架构应该是这样的:

 // Option : 1
const hotelesSchema = new Schema({
"_id" : Number, // This is optional now
"nombre": String,
"direccion": String,
"telefono": String,
"estrellas": String,
"precio": String,
"imagenes": String
})

/** Option : 2

(Or if you don't even need it at all -> _id will not be created automatically or even if you pass it won't be inserted)

const hotelesSchema = new Schema({
"nombre": String,
"direccion": String,
"telefono": String,
"estrellas": String,
"precio": String,
"imagenes": String
}, { _id: false }) */


const mainSchema = new Schema({
hoteles: [ hotelesSchema ]
}, { _id: false }); // Adding { _id: false } will not create _id on top level.

const MainColModel = mongoose.model('yourActualCollectionName', mainSchema, 'yourActualCollectionName');

代码:

const mainColObj = new MainColModel({
hoteles: [{
"_id": 123, // If you don't pass it here it won't be created.
"nombre": "Hotel Sonesta",
"direccion": "Cerritos",
"telefono": "3152020",
"estrellas": "5",
"precio": "850000",
"imagenes": "https://i.ibb.co/t2Hc8c4/Hotel-Sonesta.jpg"
},
{
"_id": 456,
"nombre": "Hotel Soratama",
"direccion": "Centro",
"telefono": "3204545",
"estrellas": "4",
"precio": "540000",
"imagenes": "https://i.ibb.co/vcyqQSf/Soratama.jpg"
}]
})

let dbResp = await mainColObj.save();

结果:

{
"_id" : ObjectId("5e1bee2a39ae20e06dc116db"), // Won't be created if you add , { _id: false } to mainSchema
"hoteles" : [
{
"_id" : 123, // You don't see _id in Option : 2
"nombre" : "Hotel Sonesta",
"direccion" : "Cerritos",
"telefono" : "3152020",
"estrellas" : "5",
"precio" : "850000",
"imagenes" : "https://i.ibb.co/t2Hc8c4/Hotel-Sonesta.jpg"
},
{
"_id" : 456, // You don't see _id in Option : 2
"nombre" : "Hotel Soratama",
"direccion" : "Centro",
"telefono" : "3204545",
"estrellas" : "4",
"precio" : "540000",
"imagenes" : "https://i.ibb.co/vcyqQSf/Soratama.jpg"
}
],
"__v" : 0
}

关于node.js - 将帖子发送到 mongoose 时数组的 QUITE _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com