Querying couchbase by passing parameter - couchbase

I am having couchbase report documents stored in below format:
{
"agree_allowed":true,
"assigned_by":"",
"assigned_to":"",
"closed":[
],
"comments_allowed":true,
"details":"Test",
"email":"",
"status":"In Progress",
"subscribed":{
"user_cfd29b81f0263a380507":true,
"user_cfd29b81f0263a380508":true,
"user_cfd29b81f0263a380509":true,
"user_cfd29b81f0263a3805010":true
},
"summary":"Test",
"time_open":0,
"timestamp":"2015-07-17T15:34:30.864Z",
"type":"report",
"user_id":"user_cfd29b81f0263a380507",
"username":"test17"
}
json contain subscribed filed, it is list of user_id who follow reports.
Problem is i have to emit report document if subscribed field contain user_id, if i pass user_id ='user_cfd29b81f0263a380507' pass as key parameter. i am wondering how can use user_id to compare in view
here is the code i write:-
function map(doc, meta) {
if (doc.type == 'report' && doc.subscribed) {
for (var user_id in doc.subscribed) {
emit(doc.user_id, doc);
}
}
}
but it didn't return expected result.
Can anybody help.

If I understand your question I think you want the ability to query the users who have subscribed.
If that is the case the view code is wrong it is submitting doc.user_id and not user_id, which is the variable you assign values to in the loop but never use. In any case I think it would be better to use a different names to avoid confusion.
function map(doc, meta) {
if (doc.type == 'report' && doc.subscribed) {
for (var subscriber in doc.subscribed) {
emit(subscriber);
}
}
}
To query the users who have subscribed you would use key=user_cfd29b81f0263a380507. The result would be:
{
"total_rows": 4,
"rows": [
{
"id": "docs",
"key": "user_cfd29b81f0263a380507",
"value": null
}
]
}

Related

How to write MySql query in Laravel

How can I write query for below table in Laravel.
Like In my app logged user id is 109
So I need whose user match with logged user in BlindDate and PaidDate table to user id.
This is User table
This is BlindDate table
THis is PaidDate table
I want need expected output is :
data[
'blind_users':[
{
'uuid':'rvfv',
'user_id':109,
'touser':[
{
'name':'james',
'email':'xxxx#gmail.com'
}
]
},
{
'uuid':'rvfv',
'user_id':109,
'touser':[
{
'name':'james',
'email':'xxxx#gmail.com'
}
]
},
],
'paid_date_users':[
{
'uuid':'rvfv',
'user_id':109,
'touser':[
{
'name':'james',
'email':'xxxx#gmail.com'
}
]
},
]
]
Hopefully you have setup the eloquent Model and its relations since you are using Laravel for this project!
If you only have the user id you can query the User model like so
return User::with('blind_date', 'paid_date')->where('id', $userId)->first();
If you have an authenticated user you can just return the user without any eager loading or just call it in you blade like auth()->user()
Ans: Use these two function in user model.
public function bind_dates(){
return $this->belongsTo('binddates','user_id');
}
public function free_paid_dates(){
return $this->belongsTo('freepaiddates','user_id');
}
Query:
Users::with('free_paid_dates','bind_dates')->where('id',$user_id)->get();
If you have a multiple user, user hasMany relation.
Please use this code, I think your code working.

Translate MYSQL query to mongodb using $Lookup

I am new to Mongodb and I have to move my SQL Queries to mongodb now. I read the document and gained enough knowledge on writing mongodb QUeries but coming from MYSQL it will take time to get expertise on framing Mongo Queries.
I am trying to convert this query to Mongo for now using $Lookup
select * from orig_tbl emas
inner join cust-tbl_after_cleanup tmclient
on emas.state = tmclient.province
and emas.cons_city = tmclient.city
where
emas.state = 'VT' and
emas.can_match_address like concat('%',tmclient.street,'%')
and emas.can_cmp_name like concat('%',tmclient.name,'%')
and emas.can_match_address like concat('%',tmclient.house_num,'%')
and emas.can_match_address like concat('%',tmclient.postal_code,'%')
and emas.cons_city like concat('%',tmclient.city,'%')
and emas.State like concat('%',tmclient.province,'%')
group by tmclient.id,tmclient.name
As you can see I'm doing an inner join on multiple conditions and then doing a partial string match on number of cloumns. From the document I got to know how to do a multiple condition join but not sure how to do partial string match
Code so far:
aggregate(
[
{
'$match' : { 'name' : 'Walmart' }
},
{
'$lookup':
{
'from': 'entity_map_all_states',
'let': { 'order_city': "$city", 'order_qty': "$province" },
'pipeline': [
{ '$match':
{ '$expr':
{ '$and':
[
{ '$eq': [ "$cons_city", "$$order_city" ] },
{ '$eq': [ "$State", "$$order_qty" ] }
]
}
}
}
],
'as': "stockdata"
}
}
] )

How to check that field exists in every array item and is non-empty

Suppose I have a contract like this:
org.springframework.cloud.contract.spec.Contract.make {
request {
method "GET"
url "/api/profiles"
headers {
header('Accept': 'application/json;charset=UTF-8')
header('Content-Type': 'application/json;charset=UTF-8')
}
}
response {
status 200
headers {
header('Content-Type': 'application/json;charset=UTF-8')
}
body(
value(
stub(
'''\
[
{
"profile": "profile1",
},
{
"profile": "profile2",
}
]
'''
),
test(
[
[
"profile" : regex(nonEmpty()),
]
]
)
)
)
}
The test of "profile" : regex(nonEmpty()) only checks that there is at least one array entry with a profile attribute that is non empty.
I would like to test that all entries have a non-empty profile.
I already tried this using test matchers:
jsonPath('$.[*].profile', byRegex(nonEmpty()))
While this checks every profile field to be non-empty, it doesn't check whether such a field actually exists.
How can I test that a profile field exists in every array entry and that each one is non-empty?
I guess the easiest way will be to use byCommand in the testMatchers section and pass the list there. Then manually assert whatever you want programmatically.

feathersjs/sequelize -> results of a 'find' are not equal to defined 'select'

Situation
In a project I have this code to select data from a table. Please note, it is working, I only don't get the result I expect.
serviceSurveyQuestions.find({
query: {
survey_id: this.survey_id,
user_id: this.$store.state.auth.user.id, //TODO move this to the hook!!
//todo make satus also not equal new
$or: [
{ status_id: process.env.mfp.statusSurveyQuestionStarted },
{ status_id: process.env.mfp.statusSurveyQuestionPlanned }
],
$sort: {
survey_question_question: 1
},
$limit: 150,
$select: [
'survey_question_question',
'survey_question_at',
'survey_question_answer',
'survey_question_details',
'survey_question_source_id',
'survey_question_source_answer_id',
'survey_question_source_user_id',
'survey_question_step',
'survey_question_dep_step',
'id'
]
}
}).then(page => {
this.listSurveyQuestions = page;
});
When I see what would be in one item of listSurveyQuestion I will see this:
{
"survey_question_question": "PEN 10 Scope vaststellen",
"survey_question_at": "2017-06-23T06:46:10.038Z",
"survey_question_answer": "",
"survey_question_details": "tester done",
"survey_question_source_id": 83499707,
"survey_question_source_answer_id": 74864,
"survey_question_source_user_id": 83488216,
"survey_question_step": 10,
"survey_question_dep_step": null,
"id": 4651,
"source_user": {
"user_id": 1005
},
"status": {
"status": "Planned"
},
"language": {
"language": "Dutch"
,
"source": {
"source": "MexonInControl - Pob - Dev (local)"
},
"survey_question": [{
"answer_type_id": 1014,
"answer_en": null,
"answer_nl": null,
"answer_explanation_en": null,
"answer_explanation_nl": null,
"survey_question_next_id": 4652
} ]
}
I know the result is comming from the configuration in my get and find hook of the service being called.
Expected Result
What I expect to happen is that the data returned is only the columns defined in the $SELECT. If I leave this as is, it will work but I'm getting to much data from the database which can be seen later as a security breach. Not with this example, but with other tables it will.
** Question **
So what do I need to change to have this functioning as expected. You could adapt the return of the service, but then I can't use the same service in other situations for the columns aren't available. Or can you pass an option to the service which will result in if (parameter = view 1) then return view 1 and so on.
** Solving **
Remark 1:
So I just see the 'cause' is a bit different. The configured hooks returns more columns from the question table which are not shown. So my guess here is that if you don't configure the includes in the find query, it will pass all includes. I need to check that and if this is the case, see if there is a option to not select the 'includes' as well.
Assuming that the hook you are referring to is setting hook.params.sequelize similar to this answer you will have to check if you included properties are also set in the $select query with something like this:
// GET /my-service?include=1
function (hook) {
const include = [];
const select = hook.params.query.$select;
// Go through all properties that are added via includes
['includeProp1', 'includeProp2'].forEach(propertyName => {
// If no $select or the include property is part of the $select
if(!select || select.indexOf(propertyName) !== -1) {
include.push({ model: ModelForIncludeProp1 });
}
});
hook.params.sequelize = { include };
return Promise.resolve(hook);
}

CouchDB get all databases and their last changes

I know that i can get all databases with
GET _all_dbs
and also the last change of a database by
GET /{db}/_changes?descending=true&limit=1
the result will be like:
{
"results":[
{
"seq":112,
"id":"20e3480f5db4802d94a8193ac2246ae7",
"changes":[
{
"rev":"2-fb8204608047ce016282acbf3239cd01"
}
],
"deleted":true
}
],
"last_seq":112
}
Now is it possible to combine these statements to get something like:
{
"results":[
{
"db1":"1-fb8204608047ce016282acbf3239cd01"
},
{
"db2":"2-fb8204608047ce016282acbf3239cd02"
},
{
"db3":"2-fb8204608047ce016282acbf3239cd03"
},
{
"db4":"2-fb8204608047ce016282acbf3239cd04"
}
]
}
where "db1" is a database name and "2-fb8204608047ce016282acbf3239cd04" is the last _rev of the database.
There is no mechanism to make any query across multiple database in couchdb.
You can however do this from your application by joining the result of multiple queries.