MongoDB query to return only embedded document

后端 未结 6 1633
别跟我提以往
别跟我提以往 2020-12-05 15:22

assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i query for and have MongoDB return only Comme

相关标签:
6条回答
  • 2020-12-05 15:38

    I also met the same problem. The way I do is use aggregate function. First unwind it and then match it.

    db.dvds.aggregate([{$unwind:"$episodes"},{$match:{"episodes.title":"Episode 1"}}])
    

    The results will be like

    { "_id" : ObjectId("5a129c9e6944555b122c8511"),
       "title" : "The Hitchhikers Guide to the Galaxy",
       "episodes" : { "title" : "Episode 1", "desc" : "..." } }
    

    It's not perfect, but then you can edit it by python.

    0 讨论(0)
  • 2020-12-05 15:42

    I think what you wanted is this:

    print db.dvds.aggregate([
      {"$unwind": "$episodes"}, # One document per episode
      {"$match": {"episodes.title": "Episode 1"} }, # Selects (filters)
      {"$group": {"_id": "$_id", # Put documents together again
                  "episodes": {"$push": "$episodes"},
                  "title": {"$first": "$title"} # Just take any title
                 }
      },
    ])["result"]
    

    The output (besides the whitespaces) is:

    [ { u'episodes': [ { u'title': u'Episode 1',
                         u'desc': u'...'
                       }
                     ],
        u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
        u'title': u'The Hitchhikers Guide to the Galaxy'
      }
    ]
    

    If you want to get rid from the u"_id", append the pipeline with:

      {"$project": {"_id": 0,
                    "episodes": "$episodes",
                    "title": "$title"}
                   }
    
    0 讨论(0)
  • 2020-12-05 15:44

    The mongodb javascript shell is documented at http://docs.mongodb.org/manual/reference/method/

    If you want to get back only specific fields of an object, you can use

    db.collection.find( { }, {fieldName:true});
    

    If, on the other hand, you are looking for objects which contain a specific field, you can sue

    db.collection.find( { fieldName : { $exists : true } } );
    
    0 讨论(0)
  • 2020-12-05 15:51

    Match more simple:

    db['dvd'].find_one( {'episodes.title': "Episode 1"},{'episodes.title': true} )
    

    Query:

    coll.find( criteria, fields );
    

    Get just specific fields from the object. E.g.:

    coll.find( {}, {name:true} );
    

    http://www.mongodb.org/display/DOCS/dbshell+Reference

    0 讨论(0)
  • 2020-12-05 15:51

    Look at db.eval:

    You should do something like:

    episode = connection['dvds'].eval('function(title){
      var t = db.dvds.findOne({"episodes.title" : title},{episodes:true});
      if (!t) return null;
      for (var i in t.episodes) if (t.episodes[i].title == title) return t.episodes[i];
    }', "Episode 1");
    

    so filtering of episodes will be on a server-side.

    0 讨论(0)
  • 2020-12-05 15:56

    This same question was asked over on the Mongo DB Google Groups page. Apparently its not currently possible but it is planned for the future.

    http://groups.google.com/group/mongodb-user/browse_thread/thread/4e6f5a0bac1abccc#

    0 讨论(0)
提交回复
热议问题