| id | query_v2 |
|---|---|
| title | Query Engine |
| sidebar_label | Query Engine |
RvnDB is powered by MongoDB.
On top of MongoDB's own query language, RvnDB's query language adds one additional layer to create a completely self-contained and portable query language. The additional rvndb layer lets you express the query language protocol version, query encoding per push data, and the query itself.
- RvnDB internally uses MongoDB to index the transactions in a structured manner. This means you can query it just like any regular MongoDB collection.
- RvnDB supports most MongoDB methods such as find, aggregate, sort, limit, project, etc.
- To query the database, you simply make a request to RvnDB with a JSON-based query language that looks like this:
Before looking at the query language, first make sure you understand how a ravencoin transaction is decoded and transformed and stored into a query-optimized format in RvnDB.
You can learn about RvnDB document format in the Indexer section: Learn Indexer
A RvnDB query is a self-contained declarative JSON query object built on top of MongoDB query language. Every query consists of 3 top level attributes:
- v: stands for "version". This documentation is based on v: 2 so always use v: 2 (v:1 has been deprecated)
- e: stands for "encoding". Key-value pairs of encoding schemes for b-prefixed attributes used in the query request and its response. (example:
{ "e": { "out.b1": "hex" } }means: "the out.b1 attribute is hex encoded")- For requests, all b-prefixed attributes are treated as "utf8" if not specified
- For responses, all b-prefixed attributes will be returned as base64 encoding unless you specify it here ("hex" or "utf8")
- q: stands for "query".
- find: MongoDB query filter object. Learn more about MongoDB query filter
- aggregate: MongoDB aggregationg pipeline stages array. Learn more about Mongodb aggregate stages
- project: MongoDB project operator for selectively returning attributes. Learn more about Mongodb projection
- sort: MongoDB sort operator. Learn more about Mongodb sort operator
- limit: MongoDB limit operator. Limit the number of results to return
- db: The array of database types to query against. The default is ["confirmed", "unconfirmed"], but you can explicitly query against only one of them, using ["confirmed"], or ["unconfirmed"]
All examples below contain "v": 2, (version 2) you should also always include the version number ("v": 2 at the moment) in all your queries to future-proof your applications.
Find 10 OP_RETURN transactions that contains "hello" as the second push data:
- The first push data (b0) is
{"op": 106}(OP_RETURN) - The second push data in UTF encoding (s1) is "hello"
{
"v": 2,
"q": {
"find": {
"out.b0": {
"op": 106
},
"out.s1": "hello"
},
"limit": 10
}
}
Find 10 transactions where the second push data is "6d02" in hex encoding
{
"v": 2,
"e": { "out.b1": "hex" },
"q": {
"find": { "out.b1": "6d02" },
"limit": 10
}
}
Find 10 transactions where the second push data is "00424554"
{
"v": 2,
"e": { "out.b1": "hex" },
"q": {
"find": {
"out.b1": "00424554"
},
"limit": 10
}
}
Find 10 transactions where the second push data is "6d02" in hex encoding, and the second push data matches "bet" in UTF8 (Note that it's combined with a full text search query for efficiency. Learn more about speeding up MongoDB regular expression queries here: How to Speed-Up MongoDB Regex Queries by a Factor of up-to 10)
{
"v": 2,
"e": { "out.b1": "hex" },
"q": {
"find": {
"$text": {
"$search": "bet"
},
"out.b1": "6d02",
"out.s2": {
"$regex": "bet", "$options": "i"
}
},
"limit": 10
}
}
Find 10 transactions with an input script with the sender qq4kp3w3yhhvy4gm4jgeza4vus8vpxgrwc90n8rhxe
{
"v": 2,
"q": {
"find": {
"in.e.a": "qq4kp3w3yhhvy4gm4jgeza4vus8vpxgrwc90n8rhxe"
},
"limit": 10
}
}
In many cases you are querying for a certain pattern in the subdocument.
For example, you may be looking for a transaction that matches certain OP_RETURN pattern in its output.
By default all RvnDB queries return the entire transaction, so you may want to ask it to only return the matched OP_RETURN output. You can do this by using MongoDB's Positional Operator $.
{
"v": 2,
"e": { "out.b1": "hex" },
"q": {
"find": { "out.b1": "6d02" },
"limit": 10,
"project": {
"out.$": 1
}
}
}
