MongoDB - Filtering the content of an internal Array in a resultset

后端 未结 5 549
再見小時候
再見小時候 2021-02-01 08:19

I\'m new using MongoDB, and I don\'t know how to solve the next problem:

I have a collection of documents like this:

{
 \"URL\": \"www.stackoverflow.com\         


        
相关标签:
5条回答
  • 2021-02-01 09:01

    It is possible to suppress keys and array elements in the returned document, but not in the way that you want.

    In your example, you can suppress the URL key with the following query, which uses the second argument to find():

    db.links.find({"TAGS.NAME" : {$all : ["question","answer"]}}, {"URL" : 0})
    

    However, I don't believe it is possible to suppress individual members of an array on the server-side with find() based on which array members were specified with $all.

    You can use $slice to return only certain members of an array, but it is position-based. For example,

    {$slice : [1, 2]}
    

    skips the first element of the array and returns up to the next two.

    0 讨论(0)
  • 2021-02-01 09:05

    Thanks Robert. I have realized that the featured I'm looking for is not implemented at this moment. Here is the link of the issue. I hope that the MongoDB cominuty implements it in a short time. Thanks!

    0 讨论(0)
  • 2021-02-01 09:07

    Generally speaking any find() operation on MongoDB returns all the documents that match the query and all documents are retrieved in their entirety. If you only want a specific section of a document then you have to do that processing on the client side.

    This is a fundamental difference between document databases and SQL databases. Typically in a document database a query returns all documents that match it while in an SQL database you can choose to return only portions of the table. Unless of course like you say you do a MapReduce but that kinda seems like overkill for your use case.

    Not to discourage you from using MongoDB but whatever project you work on consider whether NoSQL databases actually fit the bill (do they fill a requirement that SQL cannot) or whether you'd still be better going with a traditional SQL database.

    0 讨论(0)
  • 2021-02-01 09:09

    You can use aggregation framework of MongoDB.

    If you have a doc in your collection like ;

    {
     "URL": "www.stackoverflow.com",
     "TAGS": [
             {"NAME": "question", "VOTES": 3},
             {"NAME": "answer", "VOTES": 5},
             {"NAME": "problem", "VOTES": 2}
             ]
    }
    

    and you want to filter some elements of the array out you can use the aggregation sample;

    db.sof_table.aggregate
    ([
    {$unwind:'$TAGS'}, 
    {$match:{'TAGS.NAME':{$in:['answer','question']}}},
    {$group:{_id:'$URL',TAGS:{$push:'$TAGS'}}}
    ])
    

    This will result;

    {
        "result" : [
            {
                "_id" : "www.stackoverflow.com",
                "TAGS" : [
                    {
                        "NAME" : "question",
                        "VOTES" : 3
                    },
                    {
                        "NAME" : "answer",
                        "VOTES" : 5
                    }
                ]
            }
        ],
        "ok" : 1
    }
    

    as your expected result.

    0 讨论(0)
  • 2021-02-01 09:09

    This might help you.

    The $elemMatch projection operator takes an explicit condition argument. This allows you to project based on a condition not in the query, or if you need to project based on multiple fields in the array’s embedded documents.**

    https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/

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