We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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 的效果
伺服器: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 } ]
查詢資料回來的結果, check_list.store_id 裡寫的 ref 有正確參考到 store_info 的 model 但是 check_list.detail.product_id 寫的 ref 卻參考失敗
要對 subdocument 裡面下條件查詢,語法是甚麼? 例如:
check_list.find( {detail: [{check_amount: 1}]} )
或是
check_list.detail.find( {check_amount: 1} )
model.find().populate(['store_id','detail.product_id']).exec();
model.find().populate('store_id').populate('detail.product_id').exec();
check_list.find({'detail.check_amount': 1}).exec();
The text was updated successfully, but these errors were encountered:
mongoose的文檔先去全部啃個一遍吧… http://mongoosejs.com/docs/populate.html
Sorry, something went wrong.
很好的提問,支持 👍
No branches or pull requests
目的
我希望做一個 資料庫,達成 join 的效果
使用的工具
伺服器:node.js + express
模板:hogan
資料庫:MongoDB
連接資料庫工具:Mongoose
操作流程
建立 Model:
程式碼
查詢結果
遇到的問題 1
查詢資料回來的結果,
check_list.store_id 裡寫的 ref 有正確參考到 store_info 的 model
但是 check_list.detail.product_id 寫的 ref 卻參考失敗
遇到的問題 2
要對 subdocument 裡面下條件查詢,語法是甚麼?
例如:
或是
【解決】 問題 1 謝謝大大的解答,解決辦法如下
或是
【解決】 問題 2 謝謝大大們的解答,解決辦法如下
The text was updated successfully, but these errors were encountered: