MongoDB查詢問題請教
習慣沉默
習慣沉默 2017-05-02 09:17:57
0
1
528

在寫查詢的時候,感覺目前的資料結構包含一個數組,而我現在要從這個數組去匹配,然後把這個數組回傳給客戶端。
過濾出訂單中包含有我們店菜的那些訂單

{
    "_id" : "xSrtZDCqJHNbAXGPe",
    "type" : "takeaway",
    "state" : "new",
    "customer" : {
        "openId" : "oFTzvsxQNk6pYeTwTsZ-mkUGTMVM",
        "name" : "张灏哲",
        "mobile" : "13354261882",
        "campus" : "北华大学东校区",
        "building" : "第一教学楼A区",
        "room" : "301"
    },
    "items" : [ 
        {
            "_id" : "HEFFXf4SrpoSwm2ng",
            "name" : "酱香猪排饭",
            "unit" : "份",
            "price" : 10,
            "vendor" : "王记美食",
            "image" : "QjTbqCq89Gb43ER2C",
            "imageSUrl" : "/cfs/files/takeaway/QjTbqCq89Gb43ER2C/6YWx6aaZ54yq5o6S6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-s",
            "imageMUrl" : "/cfs/files/takeaway/QjTbqCq89Gb43ER2C/6YWx6aaZ54yq5o6S6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-m",
            "imageLUrl" : "/cfs/files/takeaway/QjTbqCq89Gb43ER2C/6YWx6aaZ54yq5o6S6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-l",
            "amount" : 1
        }, 
        {
            "_id" : "3WRCcJ68iPWsstqG4",
            "name" : "酱香卤肉饭",
            "unit" : "份",
            "price" : 10,
            "vendor" : "王记美食",
            "image" : "bqnnFyTM6u6J2Qnjo",
            "imageSUrl" : "/cfs/files/takeaway/bqnnFyTM6u6J2Qnjo/6YWx6aaZ5Y2k6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-s",
            "imageMUrl" : "/cfs/files/takeaway/bqnnFyTM6u6J2Qnjo/6YWx6aaZ5Y2k6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-m",
            "imageLUrl" : "/cfs/files/takeaway/bqnnFyTM6u6J2Qnjo/6YWx6aaZ5Y2k6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-l",
            "amount" : 2
        }, 
        {
            "_id" : "w4BNrrM8aWoTR2fqd",
            "name" : "板烧梅肉饭",
            "unit" : "份",
            "price" : 10,
            "vendor" : "王记美食",
            "image" : "FqTTadroGpcieaERZ",
            "imageSUrl" : "/cfs/files/takeaway/FqTTadroGpcieaERZ/5p2_54On5qKF6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-s",
            "imageMUrl" : "/cfs/files/takeaway/FqTTadroGpcieaERZ/5p2_54On5qKF6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-m",
            "imageLUrl" : "/cfs/files/takeaway/FqTTadroGpcieaERZ/5p2_54On5qKF6IKJ6aWt.jpg?token=eyJhdXRoVG9rZW4iOiJ6WlNQVEZiVnpRVmdSQUVhbnlPQVBDaDRfX0I4RDRjUTI4X3ZhdTdnZjJRIn0%3D&store=takeaway-l",
            "amount" : 1
        }
    ],
    "orderNo" : 3,
    "orderTime" : "2016-03-23 04:10:45"
}

我要從items這個數組中去匹配vendor,將匹配成功的vendor返回給對呀的商戶,比如我匹配`vendor === '王記美食',匹配成功的話,則匹配成功的話,則匹配。將這個order回傳給商家的客戶端,商家可以看到他們賣了什麼。

求大神們給個思路,sql用的不精。

我是在meteor中publish的

習慣沉默
習慣沉默

全部回覆(1)
某草草
Meteor.publish('orders', function() {
  return db.yourTable.find({ 'items.vendor': '王记美食' }, { 'items': { $elemMatch: { vendor: '王记美食' } });
});
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!