Handling ref in Node JS server with Faunadb - node.js

This might be a stupid question. I am building a server with Node JS that exposes an API to clients. I was wondering what is the recommended way to handle "ref" in the query results.
For example, I have a query like this -
q.Map(
q.Paginate(
q.Match(q.Index("network_by_creator"), q.Select("ref", q.Call(q.Function("getUserByUsername"), username))),
options
),
q.Lambda("x", q.Get(q.Var("x")))
)
This returns all the networks created by the user. An example result looks like this -
{
"before": [
{
"#ref": {
"id": "279699094284272133",
"collection": {
"#ref": {
"id": "networks",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
}
],
"data": [
{
"ref": {
"#ref": {
"id": "279699094284272133",
"collection": {
"#ref": {
"id": "networks",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
},
"ts": 1603000692605000,
"data": {
"creator": {
"#ref": {
"id": "279656553326313989",
"collection": {
"#ref": {
"id": "users",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
},
"name": "Hello2"
}
}
]
}
I was wondering should I remove the "ref" fields from the objects before sending them to the client (the "name" field can be used to search a network), or should I extract the "id" from the "ref" and send it along? In any case, can that be done through FQL, instead of querying the result and manually modifying?

A Reference is a compound value, comprised of the document's collection reference and a document ID.
You can certainly extract the document ID to send to the client, provided that when the client sends it to your API, you know which collection to use. If your API transits data for multiple collections, you should also return the collection reference name too.
Here is a query that demonstrates how to pull out the components of a Reference:
> Let(
{
ref: Ref(Collection("networks"), "279699094284272133")
},
{
collection: Select("collection", Var("ref")),
collection_Name: Select(["collection", "id"], Var("ref")),
documentID: Select("id", Var("ref"))
}
)
{
collection: Collection("networks"),
collection_Name: 'networks',
documentID: '279699094284272133'
}
Just use the appropriate Select expression to extract what you need.

Related

How can i query the access object from JSON in CouchDB

I'm using couchDB in the fabric for state database in peers. I have a json data that is stored inside the ledger
JSON DATA
"Company": {
"Associate": {
"entity": {
"name": "foo",
"role": "admin"
},
"access": [
{
"create": "allowed"
}
]
}
}
}
I have to query the data based on access json object, like these have values like "create","readonly","delete" etc..
Currently I tried this but none of the records came up.
{
"selector": {
"Company": {
"Associate": {
"access": {
"$elemMatch": {
"$eq": "create"
}
}
}
}
}
}
How can I query the data's ?
I think you want (I use dot notation for simplicity):
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": {
"$exists": true
}
}
}
}
}
...or maybe...
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": "allowed"
}
}
}
}

How to find an id in an array in mongoose using nodejs

I have data something like below:
{
"_id": "60708607143b058e101fc189",
"orders": {
"userID": "606eaa5cf67ac70cfa347fcd",
"order": [
{
"productIDs": [ "606f1f37006513258c8b59b9" ],
"vendorID": "60641cf597aed2a9a2971a3f",
"id": "60708607143b058e101fc18a"
},
{
"productIDs": [ "606f1fec006513258c8b59ba", "606f2bb2006513258c8b59bd" ],
"vendorID": "60642991015028ba0a6ce72c",
"id": "60708607143b058e101fc18b"
}
]
},
}
Is there a way to find any given vendorID in this record using nodejs and mongoose?
If you are logged in as a vendor, then you can send the vendor_id in the body of the request. To filter all orders associated to that particular vendor, you can try this:
Model.find({"orders.order":
{
"$elemMatch": {
"vendorID": req.body.vendor_id
}
}
});

Cloudant selector query array field only one get item

How can I get only one item when querying an array on CloudantDB?
Example document :
"category": {
"sub_category": [
{
"category_id": "127"
},
{
"category_id": "128"
}
],
}
query :
{
"selector": {
"sub_category": {
"$elemMatch": {
"category_id": "127"
}
}
}
}
wish result document:
"category": {
"sub_category": [
{
"category_id": "127"
}
],
}
You can't do that with Mango queries. You can accomplish something similar with a combination of a traditional view, and a show function.

How to pull embedded 1:1 data on a CouchDb document

I am using CouchDb to store my documents and figuring out how to pull 1:1 related data on a document using CouchDb views.
For this given document structure,
{
_id: 'some_id',
doc_type: 'item',
name: 'Steel Caliper'
description: 'Steel Caliper description',
classification: {
_id: 'some_classification_id'
}
}
classification: {
_id: 'some_classification_id',
doc_type: 'classification',
name: 'Hand Tools'
}
When I retrieve a list of item documents using a CouchDb view like below (called with include_docs=true)
function(doc) {
if(doc_type==='item') {
emit(doc.name, null);
}
}
I get the item document but without the classification data pulled in.
{
"total_rows": 10,
"offset": 0,
"rows": [
{
"id": "some_id",
"key": "Steel Caliper",
"value": null,
"doc": {
"_id": "some_id",
"_rev": "1-65d32fe22a0b949ff73f23c65042ae3c"
"doc_type": "item"
"name": "Steel Caliper"
"classification": {
"_id": "some_classification_id"
}
}
},
{ ... }
}
Is there a way to pull in the classification data using a view and get an output like below ?
...
"doc": {
"_id": "some_id",
"_rev": "1-65d32fe22a0b949ff73f23c65042ae3c"
"doc_type": "item"
"name": "Steel Caliper"
"classification": {
"_id": "some_classification_id",
"doc_type": "classification",
"name": "Hand Tools"
}
}
...
yes, this is called view collation. You can emit multiple docs if you are joining by _ids.
in your case you can call
function(doc) {
if(doc_type==='item') {
emit(doc.name, null);
if( doc.classification ){
emit( doc.classification._id, {_id: doc.classification._id });
}
}
}
you might still need play around with the returned result to get it into the form you want.
http://docs.couchdb.org/en/2.0.0/couchapp/views/joins.html

How to get filtered result by using Hash Index in ArangoDB?

My data:
{
"rootElement": {
"names": {
"name": [
"Haseb",
"Anil",
"Ajinkya",
{
"city": "mumbai",
"state": "maharashtra",
"job": {
"second": "bosch",
"first": "infosys"
}
}
]
},
"places": {
"place": {
"origin": "INDIA",
"current": "GERMANY"
}
}
}
}
I created a hash index on job field with the API:
http://localhost:8529/_db/_api/index?collection=Metadata
{
"type": "hash",
"fields": [
"rootElement.names.name[*].jobs"
]
}
And I make the search query with the API:
http://localhost:8529/_db/_api/simple/by-example
{
"collection": "Metadata",
"example": {
"rootElement.names.name[*].jobs ": "bosch"
}
}
Ideally, only the document containing job : bosch should be returned as a result. But for me it gives all the documents in the array name[*]. Where I am doing mistake?
Array asterisk operators are not supported by simple queries.
You need to use AQL for this:
FOR elem IN Metadata FILTER elem.rootElement.names.name[*].jobs = "bosch" RETURN elem
You can also execute AQL via the REST interface - However you should rather try to let a driver do the heavy lifting for you.

Resources