将长查询字符串直接解析到 MongoDB(就像在 SQL 中一样) [英] Parsing a long query string directly to MongoDB (just like you can in SQL)

查看:62
本文介绍了将长查询字符串直接解析到 MongoDB(就像在 SQL 中一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 或 C++ 或 Java 等中,我可以向 MySql 发送一个很长的复杂查询

In PHP or C++ or Java etc. I can send a whole long complex query to MySql

myQuery = 'SELECT * FROM BOOKINGS WHERE STATUS < 5 ORDER BY CHECKIN';
resultArray = mySql_query(myConnnect,myQuery);

现在我正在使用 MongoDB 与 MEAN 合作.有没有办法在这里做同样的事情?

Now I am working with MEAN using MongoDB. Is there a way to do the same here?

var express = require('express');
var app = express();
app.get(myDatabase, function(req, res){

    my-long-query = 'db.getCollection("receipt").aggregate([{$match:{bookingId:"94JCI"}},{$project:{"_id":0,"receiptNo":1,"amount":1}}])'
    db.myDatabase.parse-my-long-query(function(err, docs) {
        res.json(docs);
    });

});

有些查询很长,我是动态生成的现在我将它们复制粘贴到 Robomongo 测试结果.

Some of the queries are really long and I generate them dynamically Right now I copy paste them to Robomongo test the results.

有什么想法吗?

推荐答案

您可以在 node js 中使用 db.command 直接运行查询.

You can run queries directly using db.commandin node js.

聚合查询作为 db 命令采用以下形式.

Aggregation query as db command takes the below form.

https://docs.mongodb.com/manual/reference/command/aggregate/#aggregate-data-with-multi-stage-pipeline

调整您的 javascript 以将信息作为键值对发送.为 collectionnamequery 键入每个键,并将它们的值传递到下面的命令中.

Adjust your javascript to send information as key value pairs. Key each for collectionname and query and pass their values into the below command.

db.command(聚合:集合名,管道:查询,光标:{}}

不同类型的查询具有不同的语法.所以你必须在 db.command 中相应地传递键值对.

Different types of queries has different syntax. So you've to pass key value pair accordingly in db.command.

更多在这里

https://docs.mongodb.com/manual/reference/command/

这篇关于将长查询字符串直接解析到 MongoDB(就像在 SQL 中一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆