How to query item's metadata from Couchbase? - nosql

I put some data to CouchBase 1.8.1,and get it successful.But I want to query its metadata,as expiration and att_reason(non-json or json).In some document,it list the metadata with json format,for example:
{
"_id" : "contact_475",
"_rev" : "1-AB9087AD0977F089",
"_bin" : "...",
"$flags" : 0,
"$expiration" : 0,
"name" : "Fred Bloggs",
}
How can I query item's metadata?

As Pavel the most common way to access metadata in Couchbase (2.0) is using Views.
You can also use the internal TAP protocol :
http://www.couchbase.com/wiki/display/couchbase/TAP+Protocol
Could you give us more information about your use case and why you need to access meta/expiration ? And why you cannot use views (that is the recommended way to do it)
Regards
Tug

The easiest way is issuing an HTTP request to:
http://serveraddress:8091/couchBase/default/contact_475
The response should contain an X-Couchbase-Meta header with the metadata. More information is here: http://xmeblog.blogspot.co.il/2013/08/couchbase-how-to-retrieve-key.html

If you want to see the meta data in a Couchbase query you can do something like this:
SELECT meta(b).* FROM bucket b
You can also see both meta data and all other data in a query by doing something like this:
SELECT meta(b).*, * FROM bucket b

If you want to query only meta data using N1QL you could run below query, it will return all meta data about document :
select meta(bucket_name) from bucket_name
But if you want to get these information from Sync Gateway it will return to you by every GET request using REST API, the REST API also include some filtering above these meta data.

Related

How to update data in elasticsearch with using like bulkupdate in mongoDB?

I find solution to update data in elasticsearch with golang. The data is about 1,000,000+++ documents and must be specific with id of document. I can update in mongoDB with using bulk operation but I can't find it in elasticsearch it is have a operation like it? or anyony have idea to update huge of data in elasticsearch with specific id. Thanks in advance.
In general, you can use bulk API to make such bulk updates. You can either index data again using same id or just run update. You can use CURL to push the updates from command line, if you are doing it as one off update.
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
Other option is to use update_by_query, if you are setting custom fields. With update by query, you can also mix it with pipeline to update existing data.
It entirely comes down whether you are trying to run update using information from different index (in such case, you can use enrich processor, which is available in 7.5 onwards) OR if you simply want to add a new field and update it using some rule which already uses attributes available on the document.
So for different type of scenario, different options are available. Bulk API is more appropriate, when the data source is external. But if data is already available on Elasticsearch, then update by query is appropriate.
You can also look at reindexing with pipeline scripting. But again, horses for courses rule applies here as well.

How to query titan graph with gremlin queries based on integer properties

I have multiple nodes in a titan graph server with integer properties, I want to query the graph based on integer properties, the server is configured with REST so I'm querying the graph this way:
titan-server:8182/gremlin=Query
(e.g Query could be : g.V().hasLabel("Person"))
I want to fetch all person vertices with age = 30 (just an example)
This can be done in gremlin console (socket based) as follow:
g.V().hasLabel("Person").has("age",30);
but this doesn't work on rest query, it give an empty results (even if there is such a vertex with age = 30 ):
titan-server:8182/gremlin=g.V().hasLabel("Person")**.has("age",30)**;
I didn't find any docs over the internet for gremlin on rest.
Thank you for help guys
I managed to get the REST API to work by doing the following. First, as specified here make sure to change the channel in the gremlin-server.yaml config to:
channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
Then try the following post:
{
"gremlin" : "g.V().hasLabel(x).has(y,z)",
"bindings" :
{
"x" : "Person",
"y" : "age",
"z" : 30
}
}
More info on the REST API can be found here

how to insert data on mongo db through restheart api

i am using mongo DB 3.0 version and restheart API 2.0 version. now i am trying to check my queries (URI) through postman chrome interface. and i create a database(test) table(mycol) and two documents in mongo DB, when i filter that data it shows correctly but when i try to insert data into mongodb through postman or HAL Browser it shows error, can you peoples please guide me the syntax format.
Query for filter data, it gives correct result
Query for insert a document, it shows some error
and also i need to know <docid> in the URI format : /<dbname>/<collname>/<docid>[?doc_type=TYPE] what it means <docid> please explain in detail with some example
To create a document you need either to POST the collection POST /test/mycol or PUT a document PUT /test/mycol/<docid>
<docid> stands for document id. the query parameterdocid_type is optional and allows to specify the type of the <docid> in the URL, more information in the documentation Resource URI section.
For instance, if you want to create the following document { "_id": “mydoc", “message”: “hello” } you do
PUT /test/mycol/mydoc { “message”: “hello”}
or
POST /test/mycol { "_id": “mydoc", “message”: “hello” }
In the latter case, if you don’t specify the _id, it will be autogenerated as an ObjectId.
Note that you have to specify the Content-Type request header to be either application/json or application/hal+json.
For instance, using Postman you set the body to be raw and select JSON (application/json) from the dropdown on the right. You'll notice that this will add the Content-Type header to the headers.
I run into the same problem.
The problem was I used field names starting with "_"
i.e. field names like "_type", "_name".
Try to avoid such names.
I had the same problems as you. For insert you should just write your object like this:
{
"code": 20,
"name": "s",
"family": "x"
}
And set POST your method, also for update if the document exists it will be updated, otherwise it will be created.
Please look at this link for more information
https://community.boomi.com/s/article/howtointegratewithmongodbusingopensourcerestheartlibrary#jive_content_id_Scenario_1__InsertUpdate_an_Employee_record_in_Employees_collection_using_POST

CouchBase Member Login

Could anyone provide me with a great explination or tutorial around a user management couchbase database.
With SQL login would be
SELECT * FROM users WHERE email = :email AND password = :password
How would this work in couchbase, assuming my user document looks like this
{
"uid": "3",
"email": "dummy#example.com",
"password": "6a1644c781989cb3f47c8a38a0e75c6c",
"name" : "John Doe",
"jsonType" : "user"
}
My View at the moment is
function (doc, meta) {
if(doc.jsonType == "user"){
emit([doc.email, doc.password], [doc]);
}
}
And I can goto
http://localhost:8092/default/_design/dev_users/_view/login?stale=false&connection_timeout=60000&limit=10&skip=0&key=[%22dummy#example.com%22,%226a1644c781989cb3f47c8a38a0e75c6c%22]
which returns
{
"total_rows":3,"rows":
[{
"id":"user::3",
"key":["dummy#example.com","6a1644c781989cb3f47c8a38a0e75c6c"],
"value":
[{
"uid":"3",
"email":"dummy#example.com",
"password":"6a1644c781989cb3f47c8a38a0e75c6c",
"name" : "John Doe",
"jsonType" : "user"
}]
}]
}
I would just like advice on if I am approaching this the correct way. I am very new to couchbase. I have googled this but can't seem to find exactly what I'm looking for. Any help appreciated to get me off to a good start.
In noSQL(key/value) you have to pay special attention to the KEY structure design. In your case you should identify what is your loginID? Is it "email" field? If so, you can create a key based on that. e.g.
KEY: "dummy#example.com"
VALUE:
So then when user enters user id/password you can simply call one GET operation from the couchbase.
If no such loginID exists, you will not get any json value.
If loginID exists, then you will get back json document that you can use to check password and also (in case password matches) populate user specific data from that json in your login session.
NOTE: I assume you are not storing clear passwords in json, but instead using password digest with salt.
So no view functionality required here.
As for views usage, I highly recommend reading though "Basic Couchbase querying for SQL people"
also read on Creating an e-commerce platform with Couchbase 2.0
I would suggest not including the whole document in the index - i.e. change your emit to something like:
emit([doc.email, doc.password])
This will minimise the size of (and hence time taken to operate on) the index. If you later need the actual doc contents you can use a normal get operation to fetch it, using the id field of the query row. Some of the SDKs provide a method to perform this for you, for example setIncludeDocs() in the Java SDK.
Other than that this looks pretty reasonable. A detailed overview of Views is included in the Couchbase developer guide. Another good resource for complete example applications (above the standard getting started tutorials) is: http://couchbasemodels.com

mongodb - add column to one collection find based on value in another collection

I have a posts collection which stores posts related info and author information. This is a nested tree.
Then I have a postrating collection which stores which user has rated a particular post up or down.
When a request is made to get a nested tree for a particular post, I also need to return if the current user has voted, and if yes, up or down on each of the post being returned.
In SQL this would be something like "posts.*, postrating.vote from posts join postrating on postID and postrating.memberID=currentUser".
I know MongoDB does not support joins. What are my options with MongoDB?
use map reduce - performance for a simple query?
in the post document store the ratings - BSON size limit?
Get list of all required posts. Get list of all votes by current user. Loop on posts and if user has voted add that to output?
Is there any other way? Can this be done using aggregation?
NOTE: I started on MongoDB last week.
In MongoDB, the simplest way is probably to handle this with application-side logic and not to try this in a single query. There are many ways to structure your data, but here's one possibility:
user_document = {
name : "User1",
postsIhaveLiked : [ "post1", "post2" ... ]
}
post_document = {
postID : "post1",
content : "my awesome blog post"
}
With this structure, you would first query for the user's user_document. Then, for each post returned, you could check if the post's postID is in that user's "postsIhaveLiked" list.
The main idea with this is that you get your data in two steps, not one. This is different from a join, but based on the same underlying idea of using one key (in this case, the postID) to relate two different pieces of data.
In general, try to avoid using map-reduce for performance reasons. And for this simple use case, aggregation is not what you want.