node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

Is it possible to query a subdocument by any params?

Open vinimendes3 opened this issue 10 years ago • 6 comments

We are developing over the following schema:

var contractSchema = new Schema({

    isWorking: {type: Boolean, default: false},
    isLate: {type: Boolean, default: false},

    _node: {
        type: Schema.ObjectId,
        ref: 'node',
        required: true
    }
});

var staffSchema = new Schema({

    name: {type: String, required: true, trim: true},
    cardId: {type: String, trim: true},

    contracts: [contractSchema]
});

We are trying to get a list of staff where have an specific _id of _node.

We've tried to query using the following URLs:

http://localhost/api/staff?contracts._node=55d534390c2d40b61ef5270d
http://localhost/api/staff?contracts._node__equals=55d534390c2d40b61ef5270d
http://localhost/api/staff?contracts%20_node=55d534390c2d40b61ef5270d
http://localhost/api/staff?contracts%2F_node=55d534390c2d40b61ef5270d

http://localhost/api/staff?contracts._node__regex=55d534390c2d40b61ef5270d (like #84 )

and many other URLs.

Does anyone know how to solve this?

Thanks in advance !!

vinimendes3 avatar Aug 20 '15 20:08 vinimendes3

Same problem here!

LucGranato avatar Aug 20 '15 20:08 LucGranato

its pretty simple actually, take a look at this one : api/v1/messages?eventId=55bdfd9d50f375903e5eb4b4&limit=10&skip=0&sort=-_id&type=image

look how eventid field is searched. If this doesnt work, maybe the problem is that an ObjectID in mongodb is not a string directly.

romain10009 avatar Aug 21 '15 19:08 romain10009

@romain10009 Presently my _node is inside the 'contractSchema'

var contractSchema = new Schema({

   isWorking: {type: Boolean, default: false},
   isLate: {type: Boolean, default: false},

   _node: {
       type: Schema.ObjectId,
       ref: 'node',
       required: true
   }
});

and my staffSchema has a 'contractSchema' array

var staffSchema = new Schema({

    name: {type: String, required: true, trim: true},
    cardId: {type: String, trim: true},

    contracts: [contractSchema]
});

something like this

var staffSchema = new Schema({

    name: {type: String, required: true, trim: true},
    cardId: {type: String, trim: true},

    contracts: [{
       isWorking: {type: Boolean, default: false},
       isLate: {type: Boolean, default: false},

       _node: {
           type: Schema.ObjectId,
           ref: 'node',
           required: true
       }
    }]

});

my doubt remains as I'm still not able to make a search in my 'staffSchemas' where the array 'contracts' have in any position a specific _node.

This question is very similar to the issue pointed in question #84 but testing what you have suggested didn't work for me, and the fellow @sanketss84 who had asked didn't manifest if he was able to solve his problem.

@baugarten would you have any consideration about this issue?

I thank you in advance for helping!

LucGranato avatar Aug 22 '15 00:08 LucGranato

As @LucGranato said, that's our situation, we are still not able to make a search in 'staffSchema' where the array 'contracts' have in any position an specific _node id.

vinimendes3 avatar Aug 22 '15 17:08 vinimendes3

I think you had two made two queries. Once against the contracts to get there _ids and then use the array two query against the staffSchema. Here use the in query parameter. I hope that will work.

Thomas-P avatar Aug 25 '15 21:08 Thomas-P

I have the same problem, I need a query string to get all documents where allocations length value is greater than 0, is it possible ?? I defined this schema. does anybody have any suggestions? thanks

 `var allocationsSchema = new Schema({
         userName: String,
         employeeId: String,
         date: Date
    }, {_id: false});

    //create park schema
   var ParkSchema = new Schema({
   _id: {
    parkNumber: Number,
    location: String
   },
   allocations: [allocationsSchema]

});`

cesarin3134 avatar Oct 13 '16 12:10 cesarin3134