We have the following documents in couchbase:
Doc1 :
{
property1 : "someval"
name : "DOC_OF_TYPE1"
}
Doc2 :
{
property1 : "someval2"
name : "DOC_OF_TYPE1"
}
Doc3 :
{
property1 : "someval2"
name : "DOC_OF_TYPE2"
}
Is there a way to view documents of "DOC_OF_TYPE1" only ? And is there a way to delete all documents of that type from couchbase?
From Couchbase Server 4.1 onwards this is made easy through the use of the N1QL queries and DML (data manipulation language).
Firstly create a primary index on your data using N1QL, this can be done via a Couchbase SDK, Query workbench (integrated in the upcoming Couchbase 4.5 release) or the CBQ tool located in the Couchbase bin directory (/opt/couchbase/bin on linux, inside the .app file on OSX and in the install directory on Windows).
The following query creates the primary index on a bucket named 'mybucket', this allows you to perform any kind of N1QL query on a bucket:
CREATE PRIMARY INDEX ON `mybucket`;
For performance and production purposes you should create a secondary index:
CREATE INDEX 'document_name' ON `mybucket`(name);
This creates an index on every document's 'name' field. You can now efficiently select documents by their name field (This works with just the primary index but that would be slower):
SELECT *, meta().id FROM `mybucket` WHERE name = 'DOC_OF_TYPE1';
Or delete them based on their name field
DELETE FROM `mybucket` WHERE name = 'DOC_OF_TYPE2';
You can find more information about N1QL in the Couchbase Server documentation
Related
I created a view with two fields as key for the index like this [type, time]. When I tried to fetch the documents using the below query I was missing some of my documents in the resultset.
Javascript view I used looks something like this:
function(doc,meta) {
if(doc.name==type_1)
emit([type_1,doc.time],doc);
else if(doc.name==type_2)
emit([type_2,doc.time],doc);
}
here type_1 and type_2 are document types mentioned above
Query I used:
URL/VIEW_PATH?stale=update_after&inclusive_end=true&connection_timeout=60000&limit=10&skip=0&startkey=[type,start_time]&endkey=[type,end_time]&full_set=true
I had one document which should be there in the result. But I didn't get any.
Am I doing something wrong here?
Couchbase Version: 3.1.6-1904 Enterprise Edition (build-1904)
Number of documents in my bucket ~5M
I have documents with schema in a bucket:
{
"status": "done",
"id": 1
}
I want to select all documents that have status as done.
Assuming you're using Couchbase Server 4.x or greater, you can use a N1QL query to do this. For instance:
SELECT d.*
FROM mydocuments d
WHERE d.status == 'done'
You also need to create an index on status (at least--creating indexes is more complex than a StackOverflow answer can provide) like this:
CREATE INDEX ix_status ON mydocuments (status);
For more information, check out the N1QL documentation and the interactive N1QL tutorial.
While creating index I get this error:
[
{
"code": 3000,
"msg": "syntax error - at -",
"query_from_user": "create primary index on sample-partner"
}
]
If I change the bucket name to sample_partner, then it works. Using Couchbase 4.5 Enterprise edition.
Yeah that's because N1QL will interpret the - as a minus sign... You simply need to escape the bucket name using backquotes:
CREATE PRIMARY INDEX ON `sample-partner`;
It should work that way. Remember to always escape that bucket name in all N1QL queries and you should be fine. Or use the underscore in the bucket name, as an alternative :)
I want to know whether Apache Drill supports only dbo schema?? or will it support all type of schema?
I am running my system in window 8 and with latest version of Drill(1.5) with embedded mode.
I am trying to search with same storage plugin.
My Storage Plugin(for SQLServer):
{
"type" : "jdbc",
"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver",
"url" : "jdbc:sqlserver://<servername>;databaseName=<databasename>",
"username" : "<username>",
"password" : "<****>",
"enabled" : true
}
This Plugin has dbo & core schema (both have same type, no special permission).Its work for dbo schema where core schema is not working.
DBO Query:
select * from SqlServer.dbo.Attribute; //Its working.
Core Query:
select * from SqlServer.core.Users //Its not working
My question is whether Drill Supports only dbo schemna or all type of schema?
select * from <StoragePluginName>.<databaseName>.<schemaName>.<tableName>;
Ex:
seleect * from SqlServer.Test.core.Category;
This query will work for all type of user created schemas. But for dbo(default) schema
Databases name is not required. If you write database name while query through dbo
schema, it will through error.
--> But This not the good solution. Because every time we have check for
schema (default or user created schema). If it has dbo (default
schema) then database name is not required in query. And If it has
core (user created schema) schema then we have to give database name
after Storage Plugin.
I have started using couchbase recently. I am using Spring-Data couchbase for inserting Java POJOs in Couchbase. Since, Pagination and Sorting is not supported by Spring Data Couchbase project, I have tried to use couchbase java client 2.2.0-dp2.
I have inserted 8 users where ids ranging from 1 to 8.
I have wrote following code to apply pagination and sorting.
public void test() {
int offset = 5 * (1 - 1);
Statement statement = select("*").from("test").where(x("_class").eq(s("com.test.rest.entity.User"))).orderBy(Sort.asc("id")).limit(5).offset(offset);
log.info(statement.toString());
Iterator<QueryRow> result = bucket.query(Query.simple(statement)).rows();
while(result.hasNext()) {
QueryRow doc = result.next();
log.info("Document:: " + doc.value());
}
}
However, I am seeing result as below. It should be test1 to test5, though users being selected randomly. Can someone help me with that?
Document:: {“test":{"createdAt":1.443420400374E12,"firstname":"test5","_class":"com.test.rest.entity.User","type":"User","lastname":"test5"}}
Document:: {“test":{"createdAt":1.443420708495E12,"firstname":"test8","_class":"com.test.rest.entity.User","type":"User","lastname":"test8"}}
Document:: {“test:{"createdAt":1.443420386638E12,"firstname":"test2","_class":"com.test.rest.entity.User","type":"User","lastname":"test2"}}
Document:: {“test":{"createdAt":1.443420704104E12,"firstname":"test7","_class":"com.test.rest.entity.User","type":"User","lastname":"test7"}}
Document:: {“test":{"createdAt":1.443420379712E12,"firstname":"test1","_class":"com.test.rest.entity.User","type":"User","lastname":"test1"}}
It should be test1 to test5, though users being selected randomly
Looks like you are expecting to sort by first name, since id is generated by CB.
Try replacing "id" with "firstname", e.g.:
Statement statement = select("*").from("test").where(x("_class")
.eq(s("com.test.rest.entity.User")))
.orderBy(Sort.asc("firstname")).limit(5).offset(offset);
NOTE: I suspect that field name "id" collides with the metadata's id (hence not returned in your json result). Try naming it differently like "_id", "id#" etc.
After going through Spring Data Couchbase codebase, I figure out the way they are querying the n1ql. By default select(*) doesn't select id because id is not part of document. So,
N1QL Statement:
SELECT META(`test`).id AS _ID, META(`test`).cas AS _CAS, `test`.* FROM `test` WHERE `_class` = "com.test.rest.entity.User";
Couchbase Java Client code:
Statement statement = select("META(`test`).id AS _ID, META(`test`).cas AS _CAS, `test`.*").from("test").where(x("_class").eq(s("com.test.rest.entity.User"))).orderBy(Sort.asc("_ID")).limit(5).offset(offset);
NOTE: orderBy(Sort.asc("_ID")) is not required. I just kept it for sample.