mongoose MongoDB有多个模式为什么选择最后一个模式

rkkpypqq  于 9个月前  发布在  Go
关注(0)|答案(2)|浏览(124)

我做了一个架构决定,使用多个模式,因为它是允许的,而不是使用不同的数据库和必须处理集群。我注意到,当使用模型,而不是收集它使用最后一个模式。我不知道为什么。暂时我移动了有问题的模式到最后,我需要找到问题的根源或为什么会发生这种情况,这就是为什么我提出了这个问题.我不希望它以后会引起问题.对于一些逻辑,因为代码已经开发和工作之前添加新的模式.我不想有重构的代码,因为它是工作.我已经包括一些代码和下面的一个例子.我会欣赏一些方向.
后端架构搜索.cjs文件

const productSchema = new mongoose.Schema({
    name:{
    type: String,
    required: true
  },
   _id:{
    type: String,
    required: true
 }

 });
 const wishitemSchema = new mongoose.Schema({
    name:{
    type:String,
    required:true
    }

 });
 const cartitemSchema = new mongoose.Schema({
    _id:{
        type: String
        required: true.

     },
    mame:{
        type:String,
        required:true
    }

  
    }); 

 

module.exports = mongoose.model('Products',  productSchema,  'products');
module.exports = mongoose.model('Wishitems', wishitemSchema, 'wishitems');
module.exports = mongoose.model('Cartitems', cartitemSchema, 'cartitems');

example of query
api
 const Products      = require('../../../Search.cjs');
 const Wishitems     = require('../../../Search.cjs');
 const Cartitems     = require('../../../Search.cjs');

router.post('/getProducts',  async(req, res) =>{
let search = await Products.find({name: {$regex: new RegExp('^'+payload+'.*',
'i')}}).exec();

 .......
 })
 //it will give me data from the last schema.

  // If I try to use this logic above with a collection it won't work use
    .router.get(/getProducts, async(req, res =>{
     db.collection('products').find({})

    ,,,,
   })
 /*This does not work with the logic I'm using above, I have to stick with what works
 so what I did was. Note with the deletes I can use the say for example WishItems vs the 
 collection */

/* When I do this it works as expected*/

 module.exports = mongoose.model('Wishitems', wishitemSchema, 'wishitems');
 module.exports = mongoose.model('Cartitems', cartitemSchema, 'cartitems');
 module.exports = mongoose.model('Products',  productSchema,  'products');

/*Perhaps someone see something with the Products I'm not catching. I appreciate any 
 advice*/

字符串

piv4azn7

piv4azn71#

只需创建3个单独的模块。您将module.exports嵌入到Search.cjs中,因此仅导出最后一个Cartitems。这意味着ProductsWishitemsCartitems都是从cartitemSchema派生的模型。
使用当前模式进行以下更改:

产品.cjs

const mongoose = require("mongoose");
const productSchema = new mongoose.Schema({
   _id:{
      type: String,
      required: true
   },
   name:{
      type: String,
      required: true
   }
});
module.exports = mongoose.model('Product',  productSchema,  'products');

字符串

WishItem.cjs

const mongoose = require("mongoose");
const wishitemSchema = new mongoose.Schema({
   name:{
      type:String,
      required:true
   }
});
module.exports = mongoose.model('WishItem', wishitemSchema, 'wishitems');

CartItem.cjs

const mongoose = require("mongoose");
const cartitemSchema = new mongoose.Schema({
    _id:{
       type: String,
       required: true
    },
    name:{
       type: String,
       required: true
    }
}); 
module.exports = mongoose.model('CartItem', cartitemSchema, 'cartitems');

app.js

//...
//...
const Product = require('../../../Product.cjs');
const WishItem = require('../../../WishItem.cjs');
const CartItem = require('../../../CartItem.cjs');
//...
//...


以下是一些前进的提示:
1.在mongoose.model函数中不需要第三个参数,因为只要使用mongoose.model('Product', productSchema);,mongoose就会接受第一个参数'Product',并自动查找集合名的复数形式。所以'Product' --> 'products''WishItem' --> 'wishitems'等。
1.如果可以的话,不要将_id属性设置为String。Mongoose会自动添加一个属性,并将其设置为ObjectId,这有很多内置的好处。

qnzebej0

qnzebej02#

每一个module.exports =行都是前一行的 *。
考虑一下,如果你有一个这样的普通变量:

x = mongoose.model('Wishitems', wishitemSchema, 'wishitems');
 x = mongoose.model('Cartitems', cartitemSchema, 'cartitems');
 x = mongoose.model('Products',  productSchema,  'products');

字符串
您可能希望x具有最后一个赋值。
要从一个模块返回多个东西,请使用一个对象,如:

module.exports = {
    Wishitems: mongoose.model('Wishitems', wishitemSchema, 'wishitems');
    Cartitems: mongoose.model('Cartitems', cartitemSchema, 'cartitems');
    Products: mongoose.model('Products',  productSchema,  'products');
 }


然后,当需要该模块时,命名项目:

{ Wishitems, Cartitems, Products } = require('../../../Search.cjs')

相关问题