无法将复杂的javascript对象转换为查询字符串

时间:2021-09-07 00:26:35

Here is the javascript object I'm trying to convert to a query string

这是我想要转换为查询字符串的javascript对象

{$and: [{topic: categoryIds} , {$or :[ {'groups 1': {$ne: ''}}, {groups: $scope.myGroups}]}]};

Basically I'm looking to match a topic that equals a categoryIds and grab documents that have an empty groups array or that the groups array has values and matches one in the array $scope.mygroups

基本上我希望匹配一个等于categoryIds的主题,并抓取具有空组数组的文档,或者groups数组具有值并匹配数组中的一个$ scope.mygroups

My question is what would be best practice to convert this in an easily parseable format so I can append it to a GET request, and how would you go about parsing it on the express server.

我的问题是,以易于解析的格式转换它的最佳做法是什么,以便我可以将它附加到GET请求,以及如何在快速服务器上解析它。

2 个解决方案

#1


0  

I am using this code in production for querying against a server with a MongoDB backend (using angular and lodash):

我在生产中使用此代码来查询具有MongoDB后端的服务器(使用angular和lodash):

.factory('mongoQuery', function() {
  return {
    fromJson: function(json) {
      return JSON.parse(json, fromJsonReviver);
    },
    toJson: function(object) {
      return JSON.stringify(object, toJsonReplacer);
    }
  };

  function fromJsonReviver(key, value) {
    var val = value;
    if (_.isPlainObject(value)) {
      if (_.isNumber(value.$date)) {
        val = new Date(0);
        val.setUTCMilliseconds(value.$date * 1000);
      } else if (_.isString(value.$regexp)) {
        var match = /^\/(.*)\/([gimy]*)$/.exec(value.$regexp);
        val = new RegExp(match[1], match[2]);
      }
    }
    return val;
  }

  function toJsonReplacer(key, value) {
    var val = value;
    if (_.isPlainObject(value)) {
      val = _.extend({}, value);
      for (var k in value) {
        val[k] = toJsonReplacer(k, val[k]);
      }
    } else if (_.isDate(value)) {
      val = {$date: (new Date(value)).valueOf() / 1000};
    } else if (_.isRegExp(value)) {
      val = {$regexp: value.toString()};
    }
    return val;
  }
})

It includes many of the suggestions mentioned by others in the comments and supports dates and regular expressions.

它包含了评论中其他人提到的许多建议,并支持日期和正则表达式。

Other than that, if you need to send the query with a GET request, just use encodeURIComponent like others have mentioned.

除此之外,如果您需要使用GET请求发送查询,只需像其他人提到的那样使用encodeURIComponent。

Here is a working example: http://plnkr.co/edit/b9wJiUkrHMrDKWFC1Sdd?p=preview

这是一个工作示例:http://plnkr.co/edit/b9wJiUkrHMrDKWFC1Sdd?p = preview

#2


0  

What you're thinking here is basically to redevelop an API server, coupled with some mongodb database, and querying it with some format.

您在这里想到的基本上是重新开发API服务器,再加上一些mongodb数据库,并以某种格式查询它。

  1. REST is the "best practise" you're looking for. It's a standard that encapsulates some common actions to http ressources.
  2. REST是您正在寻找的“最佳实践”。它是一种将一些常见操作封装到http资源的标准。

  3. You should know that you don't need to redevelop an ecosystem based on such a standard. Full-featured REST API servers exist, some are even based on express.js. Loopback and sails.js. These one provide some extra features like
    • Model abstraction through ORM's, and database-engine agnostism
    • 通过ORM模型抽象和数据库引擎agnostism

    • Automatic REST actions, from database schema or model finition
    • 自动REST操作,来自数据库模式或模型完成

    • Advanced querying through "extended REST" ("where", "limit", "order", ...)
    • 通过“扩展REST”(“where”,“limit”,“order”,...)进行高级查询

    • Realtime with REST-like websockets
    • 实时使用类似REST的websockets

    • Client side libraries that help you query the server
    • 客户端库,可帮助您查询服务器

  4. 您应该知道,您不需要根据这样的标准重新开发生态系统。存在功能齐全的REST API服务器,有些甚至基于express.js。 Loopback和sails.js。这些提供了一些额外的功能,如通过ORM的模型抽象,以及数据库引擎agnostism自动REST操作,从数据库模式或模型完成高级查询通过“扩展REST”(“where”,“limit”,“order”,...使用类似REST的websockets实时实现可帮助您查询服务器的客户端库

  5. Some standalone exernal libraries, such as js-data or Restangular can deal with REST server pretty well, and act as a frontend connector to backend
  6. 一些独立的exernal库,比如js-data或Restangular,可以很好地处理REST服务器,并充当后端的前端连接器

Now, if I had to purely answer your question, and if you realy wanted to go with your solution, I'd just add the mongo query to a where query param on the http call with encodeURIComponent as stated before.

现在,如果我必须纯粹回答你的问题,并且如果你真的想要使用你的解决方案,我只需将mongo查询添加到http调用上的一个查询参数,如上所述,使用encodeURIComponent。

#1


0  

I am using this code in production for querying against a server with a MongoDB backend (using angular and lodash):

我在生产中使用此代码来查询具有MongoDB后端的服务器(使用angular和lodash):

.factory('mongoQuery', function() {
  return {
    fromJson: function(json) {
      return JSON.parse(json, fromJsonReviver);
    },
    toJson: function(object) {
      return JSON.stringify(object, toJsonReplacer);
    }
  };

  function fromJsonReviver(key, value) {
    var val = value;
    if (_.isPlainObject(value)) {
      if (_.isNumber(value.$date)) {
        val = new Date(0);
        val.setUTCMilliseconds(value.$date * 1000);
      } else if (_.isString(value.$regexp)) {
        var match = /^\/(.*)\/([gimy]*)$/.exec(value.$regexp);
        val = new RegExp(match[1], match[2]);
      }
    }
    return val;
  }

  function toJsonReplacer(key, value) {
    var val = value;
    if (_.isPlainObject(value)) {
      val = _.extend({}, value);
      for (var k in value) {
        val[k] = toJsonReplacer(k, val[k]);
      }
    } else if (_.isDate(value)) {
      val = {$date: (new Date(value)).valueOf() / 1000};
    } else if (_.isRegExp(value)) {
      val = {$regexp: value.toString()};
    }
    return val;
  }
})

It includes many of the suggestions mentioned by others in the comments and supports dates and regular expressions.

它包含了评论中其他人提到的许多建议,并支持日期和正则表达式。

Other than that, if you need to send the query with a GET request, just use encodeURIComponent like others have mentioned.

除此之外,如果您需要使用GET请求发送查询,只需像其他人提到的那样使用encodeURIComponent。

Here is a working example: http://plnkr.co/edit/b9wJiUkrHMrDKWFC1Sdd?p=preview

这是一个工作示例:http://plnkr.co/edit/b9wJiUkrHMrDKWFC1Sdd?p = preview

#2


0  

What you're thinking here is basically to redevelop an API server, coupled with some mongodb database, and querying it with some format.

您在这里想到的基本上是重新开发API服务器,再加上一些mongodb数据库,并以某种格式查询它。

  1. REST is the "best practise" you're looking for. It's a standard that encapsulates some common actions to http ressources.
  2. REST是您正在寻找的“最佳实践”。它是一种将一些常见操作封装到http资源的标准。

  3. You should know that you don't need to redevelop an ecosystem based on such a standard. Full-featured REST API servers exist, some are even based on express.js. Loopback and sails.js. These one provide some extra features like
    • Model abstraction through ORM's, and database-engine agnostism
    • 通过ORM模型抽象和数据库引擎agnostism

    • Automatic REST actions, from database schema or model finition
    • 自动REST操作,来自数据库模式或模型完成

    • Advanced querying through "extended REST" ("where", "limit", "order", ...)
    • 通过“扩展REST”(“where”,“limit”,“order”,...)进行高级查询

    • Realtime with REST-like websockets
    • 实时使用类似REST的websockets

    • Client side libraries that help you query the server
    • 客户端库,可帮助您查询服务器

  4. 您应该知道,您不需要根据这样的标准重新开发生态系统。存在功能齐全的REST API服务器,有些甚至基于express.js。 Loopback和sails.js。这些提供了一些额外的功能,如通过ORM的模型抽象,以及数据库引擎agnostism自动REST操作,从数据库模式或模型完成高级查询通过“扩展REST”(“where”,“limit”,“order”,...使用类似REST的websockets实时实现可帮助您查询服务器的客户端库

  5. Some standalone exernal libraries, such as js-data or Restangular can deal with REST server pretty well, and act as a frontend connector to backend
  6. 一些独立的exernal库,比如js-data或Restangular,可以很好地处理REST服务器,并充当后端的前端连接器

Now, if I had to purely answer your question, and if you realy wanted to go with your solution, I'd just add the mongo query to a where query param on the http call with encodeURIComponent as stated before.

现在,如果我必须纯粹回答你的问题,并且如果你真的想要使用你的解决方案,我只需将mongo查询添加到http调用上的一个查询参数,如上所述,使用encodeURIComponent。