发送帖子到猫鼬时数组的QUITE _id

时间:2020-01-13 03:01:09

标签: node.js mongodb mongoose database-design

我需要帮助来使用此模型创建数据库。 (它用于api猫鼬和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"
    },..]}

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

2 个答案:

答案 0 :(得分:0)

您的架构应如下所示:

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

答案 1 :(得分:0)

您的模型应如下所示:

型号:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var hotelSchema = new Schema({
    _id: false, //remove this line , it will automatic generate the _id for document which is not harmfull for you case
    hoteles: [{
        _id : true, // even if we do not mention the key _id , it will auto generate the unique _id for every object in the array of hotels
        nombre: {type : String} ,
        direccion: {type : String},
        telefono: {type : String}, 
        estrellas: {type : String},
        precio: {type : String}, 
        imagenes: {type : String}, 
    }],

});


module.exports = mongoose.model('hotel' , hotelSchema);  

尝试此代码