开发者

Retrieving only the relevant part of a stored document

开发者 https://www.devze.com 2023-03-10 14:05 出处:网络
I\'m a newbie with MongoDB, and am trying to store user activity performed on a site. My data is currently structured as:

I'm a newbie with MongoDB, and am trying to store user activity performed on a site. My data is currently structured as:

{ "_id" : ObjectId("4decfb0fc7c6ff7ff77d615e"), 
  "activity" : [
        {
                "action"    : "added",
                "item_name" : "iPhone",
                开发者_运维知识库"item_id" : 6140,
        },
        {
                "action"    : "added",
                "item_name" : "iPad",
                "item_id" : 7220,        
        }
  ], 
  "name" : "Smith,
  "user_id" : 2 
}

If I want to retrieve, for example, all the activity concerning item_id 7220, I would use a query like:

db.find( { "activity.item_id" : 7220 } );

However, this seems to return the entire document, including the record for item 6140.

Can anyone suggest how this might be done correctly? I'm not sure if it's a problem with my query, or with the structure of the data itself.

Many thanks.


You have to wait the following dev: https://jira.mongodb.org/browse/SERVER-828

You can use $slice only if you know insertion order and position of your element.

Standard queries on MongoDb always return all document.

(question also available here: MongoDB query to return only embedded document)

0

精彩评论

暂无评论...
验证码 换一张
取 消