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

query string for querying a sub-entity. Is this possible ?

Open sanketss84 opened this issue 10 years ago • 6 comments

When i perform a get request this is what i get

http://localhost:3000/api/post

[
    {
        "_id": "5529866ae1ad43bd95d6b335",
        "title": "amazon My Post",
        "content": "dasdd This is a post with some tags",
        "tags": [
            {
                "name": "amazon"
            },
            {
                "name": "tracking"
            }
        ]
    },
    {
        "_id": "55298674e1ad43bd95d6b336",
        "title": "flipkart My Post",
        "content": "dasdjkjfkajd This is a post with some tags",
        "tags": [
            {
                "name": "flipkart"
            },
            {
                "name": "tracking"
            }
        ]
    }

]

I want to perform something like this http://localhost:3000/api/post/?tags.name=amazon

it should return me

[
    {
        "_id": "5529866ae1ad43bd95d6b335",
        "title": "amazon My Post",
        "content": "dasdd This is a post with some tags",
        "tags": [
            {
                "name": "amazon"
            },
            {
                "name": "tracking"
            }
        ]
    }

]

Currently it returns me all records.

Is this possible ? or can i restructure my schema to achieve this ?

sanketss84 avatar Apr 11 '15 21:04 sanketss84

My code is as follows

Server.js

// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

// MongoDB
mongoose.connect('mongodb://localhost/NodeMongo');

// Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

//Enable CORS
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

app.use('/api',require('./routes/api'));

// Start Server
app.listen(3000);
console.log('Api is running on port 3000');

api.js

// Dependencies
var express = require('express');
var router = express.Router();

Post.methods(['get', 'post', 'put', 'delete']);
Post.register(router, '/post');

module.exports = router;

post.js

// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

//Schema
var postSchema = new mongoose.Schema({

    title: { type: 'String', required: true },
    content: { type: 'String', required: true },
    tags: [{ name : {type: 'String'} }]

});


//Return model 
module.exports = restful.model('Posts', postSchema);

sanketss84 avatar Apr 11 '15 21:04 sanketss84

Same trouble here. After some time i came up with working code :


       query = {};
       query['local.role'] = params.role;

if you do a get request with this, it will work. I want to filter this with a regex, that is not working

romain10009 avatar Apr 30 '15 22:04 romain10009

Nailed it!

localhost:8080/api/v1/users?local.email__regex=rom

will look for all elements with local['email'] containing 'rom' to write full regexp, i believe you need to add %2F instead of the / since its url params.

romain10009 avatar Apr 30 '15 23:04 romain10009

Will give it a try and get back. Thanks for your reply.

sanketss84 avatar May 01 '15 07:05 sanketss84

@sanketss84 did you have solved your problem?

LucGranato avatar Aug 21 '15 12:08 LucGranato

@romain10009 looking the #99 how is it possible to solve it?

LucGranato avatar Aug 21 '15 12:08 LucGranato