简体   繁体   English

使用MongoDB $和MEANJS

[英]Using MongoDB $and in MEANJS

I would like to use $and in my MEANJS application. 我想在我的MEANJS应用程序中使用$。 I use $resource in my service to get all the query parameters and would like to perform and AND operation. 我在我的服务中使用$ resource来获取所有查询参数,并希望执行AND操作。 My service looks like this: 我的服务如下:

//Words service used to communicate Words REST endpoints
(function () {
  'use strict';

  angular
  .module('words')
  .factory('querywordsService', querywordsService);

  querywordsService.$inject = ['$resource'];

  function querywordsService($resource) {

    var wordsData = $resource('/api/words/?syllableCount=:count&syllableStructur=:struct&frequency=:freq&emphasis=:emph',
    { count: '@count', struct: '@struct', freq: '@freq', emph: '@emph' },
      {
        update: {
          method: 'PUT'
        }
      });

    return {
      wordsData: wordsData
    };
  }
})();

In my Ctrl i use functions to set @count and the other parameters to build the query i need. 在我的Ctrl中我使用函数来设置@count和其他参数来构建我需要的查询。 What i would like to accomplish is to have multiple values for @count and the others. 我想要实现的是为@count和其他人提供多个值。 I this possible in the frontend or do i need to do it in the backend (if so, can you point me in the right direction on where to start there?) 我可能在前端或者我需要在后端进行此操作(如果是这样,你能指出我在哪里开始的正确方向吗?)

You just need the operator in of mongoose for your case. 你只需要运营商inmongoose为您的案件。

Let's say, you wanna get the results where syllableCount = 1 or 2 and syllableStructur = einfach or komplex : 让我们说,你想得到的结果是syllableCount = 12syllableStructur = einfachkomplex

  // Inside your controller function
  YourModel.
  find({}).
  where('syllableCount').in([1, 2]).
  where('syllableStructur').in(['einfach', 'komplex']).
  exec(callback);

From your frontend, you would pass these variables as arrays of possible values. 从前端,您可以将这些变量作为可能值的数组传递。 Then just change the [1, 2] and ['einfach', 'komplex'] from my example for your req.body.syllableCount or whatever you name it. 然后从你的例子中为你的req.body.syllableCount或你命名的任何东西改变[1, 2]['einfach', 'komplex']

If you are using MEAN stack(you need to applied Mongoose & Express), I will recommend a very powerful library -- express-restify-mongoose . 如果你正在使用MEAN堆栈(你需要应用Mongoose和Express),我会推荐一个非常强大的库 - express-restify-mongoose Please follow the link to use it. 请点击链接使用它。 After applied this library into your project, it will automatically generate RESTful URL webservice for you to CRUD data in mongoDB by HTTP requests. 将此库应用到您的项目后,它将自动为您通过HTTP请求为mongoDB中的CRUD数据生成RESTful URL Web服务。

After you applied express-restify-mongoose, you can use another library -- request and follow this example to query mongoDB with $and criteria: 应用express-restify-mongoose后,您可以使用另一个库 - 请求并按照此示例使用$和条件查询mongoDB:

var url = 'http://localhost:7100';

// Here is the $and query
var qs = {
    query: JSON.stringify({
        $and: [
            {name: 'Tom'},
            {age: {$gt: 20}}
        ]
    })
};


request.get({
    baseUrl: url,
    url: '/api/v1/MyModels',  // URL is generated by express-restify-mongoose
    qs: qs,
    json: true
}, function (error, response, body) {
    if (!error && (response.statusCode == 200)) {
        console.log(body.length);
        console.log(body);
    } else {
        console.log(error);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM