summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/query_projection.cpp
blob: 5e8cda0e5ae72e6874914502ca2110b53c528f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mongo/db/query/query_projection.h"

#include "mongo/db/exec/working_set.h"
#include "mongo/db/jsobj.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

    //
    // .find() syntax incl/excl projection
    // TODO: Test.
    //

    class InclExclProjection : public QueryProjection {
    public:
        virtual ~InclExclProjection() { }

        Status project(const WorkingSetMember& wsm, BSONObj* out) {
            BSONObjBuilder bob;
            if (_includeID) {
                BSONElement elt;
                if (!wsm.getFieldDotted("_id", &elt)) {
                    return Status(ErrorCodes::BadValue, "Couldn't get _id field in proj");
                }
                bob.append(elt);
            }

            if (_fieldsInclusive) {
                // We only want stuff in _fields.
                for (unordered_set<string>::const_iterator it = _fields.begin();
                     it != _fields.end(); ++it) {
                    BSONElement elt;
                    if (!wsm.getFieldDotted(*it, &elt)) {
                        return Status(ErrorCodes::BadValue, "no field " + *it + " in wsm to proj");
                    }
                    bob.append(elt);
                }
            }
            else {
                // We want stuff NOT in _fields.  This can't be covered, so we expect an obj.
                if (!wsm.hasObj()) {
                    return Status(ErrorCodes::BadValue,
                                  "exclusion specified for projection but no obj to iter over");
                }
                BSONObjIterator it(wsm.obj);
                while (it.more()) {
                    BSONElement elt = it.next();
                    if (_fields.end() == _fields.find(elt.fieldName())) {
                        bob.append(elt);
                    }
                }
            }

            *out = bob.obj();
            return Status::OK();
        }

    private:
        friend class QueryProjection;

        // _id can be included/excluded separately and is by default included.
        bool _includeID;

        // Either we include all of _fields or we exclude all of _fields.
        bool _fieldsInclusive;
        unordered_set<string> _fields;
    };

    // static
    Status QueryProjection::newInclusionExclusion(const BSONObj& obj, QueryProjection** out) {
        auto_ptr<InclExclProjection> qp(new InclExclProjection());

        // Include _id by default.
        qp->_includeID = true;

        // By default include everything.
        bool lastNonIDValue = false;

        BSONObjIterator it(obj);
        while (it.more()) {
            BSONElement elt = it.next();
            if (mongoutils::str::equals("_id", elt.fieldName())) {
                qp->_includeID = elt.trueValue();
            }
            else {
                bool newFieldValue = elt.trueValue();
                if (qp->_fields.size() > 0) {
                    // make sure we're all true or all false otherwise error
                    if (newFieldValue != lastNonIDValue) {
                        return Status(ErrorCodes::BadValue, "Inconsistent projection specs");
                    }
                }
                lastNonIDValue = newFieldValue;
                qp->_fields.insert(elt.fieldName());
            }
        }

        qp->_fieldsInclusive = lastNonIDValue;
        *out = qp.release();
        return Status::OK();
    }

}  // namespace mongo