1

I would like to find out how to do the below, without actually doing the DB Query.. i.e. I would like to know if "someData" would pass "whereClause" without putting it in a table and asking for it back again. I.e. Run the logic inside findOne without the overhead of insert and select. Just to make it more fun, please consider that I need to have it thread safe, thats why im messing with a Guid kinda thing below.. Also please note the where clause is probably gonna be more complex than the below, like { a : { $ne : 1 } }

Given Source:

someData = { a: 1, b: 2 };
whereClause = { b: 2 };

My code that needs fixing:

someData.GUID = ObjectId();

// DB QUERY - insert
db.workspace.insert(someData);

whereClause.GUID = inputsValues.GUID;

// Check if the data passes the whereClause
// DB QUERY - findOne
var whereResult = db.workspace.findOne(whereClause);

// DB QUERY - remove
db.workspace.remove({ "GUID": whereClause.GUID });

if (whereResult == null)
  alert("Fail");
else 
  alert("Pass");

In SQL what I want can be expressed kinda like this: (pseudo syntax)

if (
    Select Count(*) from ((Select 1 as A, 2 as B) Data where B = 2) Result
) = 1 then 'pass' else 'fail'

The above query never actually touches a table - that is my main goal.

user230910
  • 2,353
  • 2
  • 28
  • 50
  • ummm, anyone have anything useful to contribute? – user230910 Jul 14 '14 at 14:18
  • Are you looking to just test if your queries will work for development purposes? Or is this supposed to be behavior in your application? – JasonMArcher Jul 14 '14 at 19:29
  • this is behaviour in my application - i need to run several queries, and an (insert, find, remove) cycle on each request adds a lot to the overhead in my application. This test checks if the rest of the 400 lines of javascript will be execute correctly, so if I can quit early, I save very many headaches later. Just for context, i am looking to do this 500+ times a second, so each database query in my code really needs to justify its existence. Later on I do other much more expensive queries, and this can prevent that, but it needs to be FAST – user230910 Jul 15 '14 at 13:15
  • Is this something really difficult to do in MongoDB ? Or is it so trivial that no one thinks it's worth explaining? – user230910 Jul 17 '14 at 13:43
  • 1
    Since you are trying to validate your where clause without actually sending it to the MongoDB server, this is really an application/driver issue. I'm not aware of any helper for that; it is possible to construct a query which may be syntactically valid in your driver but still not make any sense as a server query. This seems like over-optimisation :). I would suggest looking into the [new Bulk API in MongoDB 2.6](http://blog.mongodb.org/post/84922794768/mongodbs-new-bulk-api); I think effectively what you want is ordered bulk writes that fail on the first error. – Stennie Jul 17 '14 at 22:20
  • FWIW, if you're looking to query "like" MongoDB there are a few JavaScript options, eg: [what Javascript library can evaluate MongoDB-like query predicates](http://stackoverflow.com/questions/15397668/what-javascript-library-can-evaluate-mongodb-like-query-predicates-against-an-ob). These may suffice if you have simple enough queries to validate. – Stennie Jul 31 '14 at 06:25
  • Stennie, I would like to give you credit, but how? – user230910 Aug 27 '14 at 06:12

2 Answers2

0

Ok so I took this question to MongoDB support (10gen) and asked them for advice. It seems there is no syntactic way of saying what I want to say. Their suggestion is to use a separate mongodb instance as close to the application as possible, and to use that for only this purpose avoiding any potential slowing down due to locking etc.

So the solution is: Make a new MongoDB instance local to the app, and run thses queries in there. That would be the only way to do this.

I'll leave the question open to anyone that can provide a better solution.

user230910
  • 2,353
  • 2
  • 28
  • 50
0

Ok, so Stennie (in a comment) above has provided clues to what I think is the best answer for this situation.

He provided the link below: what Javascript library can evaluate MongoDB-like query predicates against an object?

This has led me to 2 javascript libraries:
1) Mingo - https://github.com/kofrasa/mingo
2) Sift - https://github.com/crcn/sift.js

Mingo has the advantage of being able to evaluate MongoDB syntax, where Sift seems to be more complete. For my situation Mingo is perfect and exactly what I was looking for.

(I don't know how to give Stennie credit for this answer, but it really belongs to him)

Community
  • 1
  • 1
user230910
  • 2,353
  • 2
  • 28
  • 50
  • And off topic - if you want to do this from c# land - then you could use a library "Jurassic" to run a javascript engine inside your app - then no need to even go to talk to mongodb.. – user230910 Aug 03 '14 at 05:09
  • 1
    I think of the two tools recommended, Mingo is the most feature complete and closest to what MongoDB offers. Full disclosure: I am the author :) – kofrasa Apr 15 '15 at 17:38
  • Yeah, a couple of months later, im not regretting going down the mingo road, thanks for the effort in making it! – user230910 Apr 16 '15 at 06:28