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

后端 未结 5 693
再見小時候
再見小時候 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.

提交回复
热议问题