Node.js 发送post到mongoose时数组的完全id

Node.js 发送post到mongoose时数组的完全id,node.js,mongodb,mongoose,database-design,Node.js,Mongodb,Mongoose,Database Design,我需要帮助用这个模型创建数据库。 (对于api mongoose和express,我可以使用mongoshell或mongoose模式) 但是当我将数组发布到MongoDB时,会自动为我的数组创建\u id:“”,我可以删除它吗?我需要\u id,但对于数组中的文档,我获得如下内容: {"_id": "5e181fed9fc1883a69797e3a", "hotels":[ { "name" : "Hotel Emperador", "stars" : 4,

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

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

{"_id": "5e181fed9fc1883a69797e3a", 
  "hotels":[
    {
    "name" : "Hotel Emperador",
    "stars" : 4,
    "price" : 1596,
    "imagen" : "https://i.ibb.co.jpg",
    "id": 1
    },...]}
 // 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');
我需要这样的东西,因为这对我来说很难。查找具有上一代码的文档

{
"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"
    },..]}
我知道模型模式可能会有帮助,但尝试过,但没有成功
你能帮我吗?

你的模式应该是这样的:

{"_id": "5e181fed9fc1883a69797e3a", 
  "hotels":[
    {
    "name" : "Hotel Emperador",
    "stars" : 4,
    "price" : 1596,
    "imagen" : "https://i.ibb.co.jpg",
    "id": 1
    },...]}
 // 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
}
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);  
结果:

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
}
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);  

您的架构应该如下所示:

{"_id": "5e181fed9fc1883a69797e3a", 
  "hotels":[
    {
    "name" : "Hotel Emperador",
    "stars" : 4,
    "price" : 1596,
    "imagen" : "https://i.ibb.co.jpg",
    "id": 1
    },...]}
 // 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
}
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);  
结果:

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
}
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);  

您的模型应如下所示:

型号:

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
}
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);  

尝试此代码

您的模型应如下所示:

型号:

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
}
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);  

试用此代码

您到底需要什么?那么,您是需要在hotels数组的每个对象中使用
\u id
,还是需要在插入时删除它?这就是问题所在吗?我只需要嵌入数组的对象的id。我的数组不需要。你的意思是你想插入由你生成或分配的
\u id
对象吗?我需要mongo db不自动生成数组id,但为我的嵌套对象生成id。是的。有可能吗?很抱歉,延迟了,我尝试使用你的代码并重新生成了此id“消息”:“文档在保存之前必须有一个_id”//带有{u id:“false”}into mainSchema不允许将请求发布到mongodb。您到底需要什么?那么您需要在hotels数组的每个对象中使用
\u id
,还是需要在插入时删除它?这就是问题所在吗?我只需要嵌入我的对象的id,而我的数组不需要。您的意思是要插入生成了
\u id
的对象吗由您分配?我需要mongo db不自动生成id我的数组,但为我的嵌套对象生成id是的。有可能吗?很抱歉延迟,我尝试使用您的代码并重新收到此“消息”:“文档在保存前必须有_id”//带有{u id:“false”}进入主模式不允许请求发布到mongodb。@Steven Ramirez:如果您需要在顶层发生相同的事情,即,如果您不需要
“\u id”:ObjectId(“5e1bee2a39ae20e06dc116db”)
从上面的
结果
,那么您需要添加
{id:false}
to
mainSchema
@Steven Ramirez:如果您需要在顶层发生相同的事情,即;如果您不需要
“\u id”:ObjectId(“5e1bee2a39ae20e06dc116db”)
从上面的
结果
,那么您需要将
{id:false}
添加到
mainSchema
您好,我尝试了您的代码,但是我的VS(编辑说,_idno it valid为true,然后我更改了其作品的编号,但当我发送post请求时:“消息”:“文档在保存之前必须有一个_id”,因此现在将有文档以及您的
hotels
数组的_id。这是正确的,因此您可以访问所有文档。@StevenRamirezhello,我尝试了您的代码,但我的VS(编辑说,_idno it valid为true,然后我更改了它的作品编号,但当我发送post请求时:“消息”:“文档在保存之前必须有一个_id”,因此现在将有文档以及您的
hotels
数组的_id。这是正确的,因此您可以访问所有文档。@StevenRamirez)