MongoDB查询问题请教
習慣沉默
習慣沉默 2017-05-02 09:17:57
0
1
619

在写查询的时候,感觉目前的数据结构中包含一个数组,而我现在要从这个数组中去匹配,然后把这个数组返回给客户端。
过滤出订单中包含有我们店菜品的那些订单

{
    "_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的

習慣沉默
習慣沉默

répondre à tous(1)
某草草
Meteor.publish('orders', function() {
  return db.yourTable.find({ 'items.vendor': '王记美食' }, { 'items': { $elemMatch: { vendor: '王记美食' } });
});
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal