Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【已解決】我希望做一個 資料庫,達成 join 的效果 #27

Open
weweaaa opened this issue Aug 20, 2017 · 2 comments
Open

Comments

@weweaaa
Copy link

weweaaa commented Aug 20, 2017

目的

我希望做一個 資料庫,達成 join 的效果

使用的工具

伺服器:node.js + express
模板:hogan
資料庫:MongoDB
連接資料庫工具:Mongoose

操作流程

建立 Model:

var store_info = mongoose.model(
  'store_info', 
  mongoose.Schema({
    store_name: String,
    store_phone: String,
    store_address: String,
    is_delete: {type: Boolean, default: false}
  }, {
    timestamps: {createdAt: 'insert_date', updatedAt: 'update_date'}
  }
), 
'store_info');

var product_info = mongoose.model(
  'product_info', 
  mongoose.Schema({
    product_name: String,
    product_type: String,
    product_price: Number,
    product_size: String,
    product_amount: Number,
    product_picture: String,
    product_date: Date,
    is_delete: {type: Boolean, default: false}
  }, {
    timestamps: {createdAt: 'insert_date', updatedAt: 'update_date'}
  }
), 
'product_info');

var check_list = mongoose.model(
  'check_list', 
  mongoose.Schema({
    store_id: {type: Schema.Types.ObjectId, ref: 'store_info'},
    check_date: Date,
    is_delete: {type: Boolean, default: false},
    detail: [new Schema({
      product_id: {type: Schema.Types.ObjectId, ref: 'product_info'},
      product_price: Number,
      check_amount: Number
    }, {
      timestamps: {createdAt: 'insert_date', updatedAt: 'update_date'}
    })]
  }, {timestamps: {createdAt: 'insert_date', updatedAt: 'update_date'}}
),
'check_list');

程式碼

var model = mongoose.model('check_list');
model.find().populate('store_id').exec();

查詢結果

docs:
[ { _id: 5997e814a524cd07c03f2bd7,
    update_date: 2017-08-19T07:26:12.566Z,
    insert_date: 2017-08-19T07:26:12.566Z,
    __v: 0,
    store_id:
     { _id: 5997e433366d4513b0e420dd,
       update_date: 2017-08-19T07:09:39.783Z,
       insert_date: 2017-08-19T07:09:39.783Z,
       __v: 0,
       store_name: '店2',
       store_phone: '452674',
       store_address: '陸2',
       is_delete: false },
    check_date: 2017-08-19T00:00:00.000Z,
    detail: [ [Object] ],
    is_delete: false } ] 

docs[0].detail:
[ { _id: 5997e814a524cd07c03f2bd8,
    check_amount: 1,
    product_price: 1,
    product_id: 5973311022fbb51034da23d6 } ]

遇到的問題 1

查詢資料回來的結果,
check_list.store_id 裡寫的 ref 有正確參考到 store_info 的 model
但是 check_list.detail.product_id 寫的 ref 卻參考失敗

遇到的問題 2

要對 subdocument 裡面下條件查詢,語法是甚麼?
例如:

check_list.find(
  {detail: [{check_amount: 1}]}
)

或是

check_list.detail.find(
  {check_amount: 1}
)

【解決】 問題 1 謝謝大大的解答,解決辦法如下

model.find().populate(['store_id','detail.product_id']).exec();

或是

model.find().populate('store_id').populate('detail.product_id').exec();

【解決】 問題 2 謝謝大大們的解答,解決辦法如下

check_list.find({'detail.check_amount': 1}).exec();
@kudorori
Copy link

mongoose的文檔先去全部啃個一遍吧…
http://mongoosejs.com/docs/populate.html

@weweaaa weweaaa changed the title 我希望做一個 資料庫,達成 join 的效果 【已解決】我希望做一個 資料庫,達成 join 的效果 Aug 20, 2017
@dca
Copy link
Contributor

dca commented Aug 20, 2017

很好的提問,支持 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants