MongoVUE使用

Using Regular Expressions in Find|Fields|$where|Sorts with MongoVUE

1.Find:

Let us consider a collection of “users”. To find all the users where the username begins with the character “a”, we would write the following in the shell

db.users.find({"username":/^a/});

In MongoVUE, to fetch similar results from the “users” collection, you need to make a slight modification to the JSON query. After selecting “users” collection in the tree, you’ll need to enter the following (valid) JSON query in the Find popout box:

{"username": new RegExp("^a")}

Multiple conditions like this:

  {Fldname:new RegExp("/^sgnrgn_/i"),Typename:new RegExp("/var/i")}

2.Fields:

At times you want to reduce extra data sent by the server to the client. For example, let us say you have an “Fieldview” collection and you wish to retrieve only Fldname and Typename.

In a SQL database, you would achieve this by

   SELECT Fldname,Typename FROM Fieldview

instead of

   SELECT * FROM Fieldview

To achieve the same affect in MongoDB, simply mention the items you wish to fetch (within the resulting documents) in the “Fields” popout box.

3.$where:

In MongoDB, “$where” is used to specify arbitrary Javascript that allows one to run more specialized queries, especially the boolean “OR” criteria.

Lets continue with the example of our “address” collection that we first discussed under Find. Documents in this collection contain address information, for example:

{
	"Street":"123 ABC Street",
	"Street2": "Search Boulevard",
	"City": "Allentown",
	"State": "PA",
	"Country": "United States"
}

Now, if we were to fetch all the address in Allentown and Pittsburgh only, we would specify a JavaScript statement (condition, that evaluates to true or false) as follows:

 

This same can also be expressed in the form of a JavaScript function:

 

Please note: You could achieve the same results by specifying your JavaScript inside the “Find” query (see pic below). Also note that if you specify such a condition in both the “Find” popout box, and the “Where” popout box, then the JavaScript in “Where” takes precedence and overwrites those specified under “Find”.

4.Sorts:

Sort is easy. When you want your results to be sorted in a particular order, you just specify the sorting criteria in a Json object.

Lets say you want to retrieve all documents from our “address” collection in the alphabetical order of “State” names, you would simply enter the appropriate Json in the Sort popout box as shown below:

你可能感兴趣的:(mongoDB)