finding document in mongodb with specific id and username - json

{
"data": [
{
"_id": 555,
"username": "jackson",
"status": "i am coding",
"comments": [
{
"user": "bob",
"comment": "bob me "
},
{
"user": "daniel",
"comment": "bob the builder"
},
{
"user": "jesus",
"comment": "bob the builder"
},
{
"user": "hunter",
"comment": "bob the builder"
},
{
"user": "jeo",
"comment": "bob the builder"
},
{
"user": "jill",
"comment": "bob the builder"
}
]
}
]
}
so i want to get the result with _id :555 and user:bob i tried with below code but i cant make it work it returns empty array
app.get('/all',function(req , res){
db.facebook.find({_id:555},{comments:[{user:"bob"}]},function(err,docs){res.send(docs,{data:docs});});
} );
i want the result to be like this listed below with the comment with user:bob
{
"_id": 555,
"username": "jackson",
"status": "i am coding",
"comments": [
{
"user": "bob",
"comment": "bob me "
}
]
}

Only aggregate or mapReduce could exclude items from subarray in output. Shortest is to use $redact:
db.facebook.aggregate({
$redact:{
$cond:{
if:{$and:[{$not:"$username"},{$ne:["$user","bob"]}]},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
})
Explanation:
$reduct would be applied to each subdocument starting from whole document. For each subdocument $reduct would either prune it or descend. We want to keep top level document, that is why we have {$not:"$username"} condition. It prevents top level document from pruning. On next level we have comments array. $prune would apply condition to each item of comments array. First condition {$not:"$username"} is true for all comments, and second condition {$ne:["$user","bob"]} is true for all subdocuments where user!="bob", so such documents would be pruned.
Update: in node.js with mongodb native driver
db.facebook.aggregate([{
$redact:{
$cond:{
if:{$and:[{$not:"$username"},{$ne:["$user","bob"]}]},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}], function(err,docs){})
One more thing: $prune is new operator and available only in MongoDB 2.6

Related

IF statement in Couchbase Map of view - I'm sure I'm missing something simple

I'm trying to limit the map in my view to a specific set of documents by either having the id "startsWith" a string or based on there being a specific node in the JSON> I can't seem to get a result set once I add an IF statement. The reduce is a simple _count:
function(doc, meta) {
if (doc.metricType == "Limit_Exceeded") {
emit([doc.ownedByCustomerNumber, doc.componentProduct.category], meta.id);
}
}
I've also tried if (doc.metricType) and also if(meta.id.startsWith("Turnaway:")
Example Doc:
{
"OvidUserId": 26105400,
"id": "Turnaway:00005792:10562440",
"ipAddress": "111187081038",
"journalTurnawayNumber": 10562440,
"metricType": "Limit_Exceeded",
"oaCode": "OA_Gold",
"orderNumber": 683980,
"ovidGroupID": 3113900,
"ovidGroupName": "tnu999",
"ovidUserName": "tnu999",
"ownedByCustomerNumber": 59310,
"platform": "Lippincott",
"samlString": "",
"serialName": "00005792",
"sessionID": "857616ee-dab7-43d0-a08b-abb2482297dd",
"soldProduct": {
"category": "Multidisciplinary Subjects",
"name": "Custom Collection For CALIS - LWW TA 2020",
"productCode": "CCFCCSI20",
"productNumber": 33410,
"subCategory": "",
"subject": "Multidisciplinary Subjects"
},
"soldToCustomer": {
"customerNumber": 59310,
"keyAccount": false,
"name": "Tongji University"
},
"turnawayDateTime": "2022-05-04T03:01:44.600",
"usedByCustomer": {
"customerNumber": 59310,
"keyAccount": false,
"name": "Tongji University"
},
"usedByCustomerNumber": 59310,
"yearMonth": "202205"
},
"id": "Turnaway:00005792:10562440"
}
Thanks,
Gerry
Found it (of course after posting the question) The second component of the Key in the emit has to exist. I entered doc.componentProduct.category instead of doc.soldProduct.Category.

Microsoft Power-Automate Select operation does not work as intended

I've got a Select-Operation on an an object that contains one key more than once.
It's practically two versions of one object in one JSON object.
I want to get the id of both of those Objects.
When i inspect the Object, i can clearly see the two different Id's, but the Select-Operation returns only one of them twice.
This is the original Object:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:06:04",
"ID": 10,
}
]
The Select-Operation looks like this:
{
"inputs": {
"from": "#body('Rest')?['value']",
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
}
}
And it returns:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
}
]
I don't really understand what's going on.
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
is wrong, it should select item()?['ID']

Render Nested json objects in React Native

In my render() method i need to parse nested json objects. See the portion of render and json structure below.
I access Last Name with {params.name_last}.
How will i access items under team, like team.name_first
render() {
let { params } = this.props.navigation.state
<Text>{params.name_last}</Text>
}
[
{
"id": 1,
"name_first": "Name first 1",
"name_middle": "",
"name_last": "Name last 1",
"name_suffix": "",
"phone": "888-8888",
"fax": "888-8888",
"updated_at": "2015-11-02T21:42:42.000Z",
"team": [
{
"id": 16,
"name_first": "aaa",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
},
{
"id": 28,
"name_first": "aaa",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
},
{
"id": 29,
"name_first": "aaa ",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
}
]
}
]
Since team is an array, you need to either access a specific entry in the array, or iterate over the entire thing.
To reach a specific property in the nested array entry (assuming you want the object at index i):
params.team[i].name_first
To create an array of first names:
params.team.map(x => x.name_first)

couchdb ; how get documents directly in first level of json, and not grouped inside value - viewWithList

I have a design document: 'accounts',and the view: 'accounts-view'
the view's content is:
function (doc) {
emit( doc._id, doc);
}
And my code in express is:
db.view('accounts', 'accounts-view', function(err, body) {
if (err) throw error;
res.json(body.rows);
});
Result is:
[
{
"id": "8767d3474a0e80dd0ab7d0b0580065af",
"key": "8767d3474a0e80dd0ab7d0b0580065af",
"value": {
"_id": "8767d3474a0e80dd0ab7d0b0580065af",
"_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
"type": "accounts",
"lastname": "Kitchen",
"firstname": "Peter"
}
},
{
"id": "8767d3474a0e80dd0ab7d0b058006e3c",
"key": "8767d3474a0e80dd0ab7d0b058006e3c",
"value": {
"_id": "8767d3474a0e80dd0ab7d0b058006e3c",
"_rev": "1-bcab94bb253c83b4951a787c253896f5",
"type": "accounts",
"lastname": "Kolner",
"firstname": "John"
}
}
]
How i can get just something like this: ( just printing all is inside value for every row)
[
{
"_id": "8767d3474a0e80dd0ab7d0b0580065af",
"_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
"type": "accounts",
"lastname": "Kitchen",
"firstname": "Peter"
},
{
"_id": "8767d3474a0e80dd0ab7d0b058006e3c",
"_rev": "1-bcab94bb253c83b4951a787c253896f5",
"type": "accounts",
"lastname": "Kolner",
"firstname": "John"
}
]
UPDATE:
I've follow Domonique's suggestions ; and now I have a new view, that emit just the id (so i can save space on disk and retrive de doc with the parameter "include_docs=true" on the view):
function(doc) {
if (doc.type && doc.type=='accounts') {
emit( doc._id);
}
}
and a new list:
function(head, req) {
provides('json', function() {
var results = [];
while (row = getRow()) {
//results.push(row.value);
results.push(row.doc);
}
send(JSON.stringify(results));
});
}
Finally i get the records with:
http://127.0.0.1:5984/crm/_design/crmapp/_list/accounts-list/accounts-view?include_docs=true
and the result is:
[
{
"_id": "8767d3474a0e80dd0ab7d0b0580065af",
"_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
"type": "accounts",
"lastname": "Kitchen",
"firstname": "Peter"
},
{
"_id": "8767d3474a0e80dd0ab7d0b058006e3c",
"_rev": "1-bcab94bb253c83b4951a787c253896f5",
"type": "accounts",
"lastname": "Kolner",
"firstname": "John"
},
{
"_id": "8767d3474a0e80dd0ab7d0b058008e9a",
"_rev": "1-86078f00be82b97499a0f52488cefbbf",
"lastname": "Tower",
"firstname": "George",
"type": "accounts"
}
]
my app node express updated:
db.viewWithList('crmapp', 'accounts-view','accounts-list', {"include_docs":"true"} , function(err, body) {
if (err) throw err;
res.json(body);
});
with this list , I don't need more reduce it on express project, it's ok ?
How to udate my list or view to get by id ? it'not working just adding id on the url ; like this:
http://127.0.0.1:5984/crm/_design/crmapp/_list/accounts-list/accounts-view?include_docs=true&_id=8767d3474a0e80dd0ab7d0b058006e3c
I get all the records and not the only one by id
To answer your question here, you should simply map the array and only include the value portion:
db.view('accounts', 'accounts-view', function(err, body) {
if (err) throw error;
res.json(body.rows.map(function (row) {
return row.value;
}));
});
Since it's apparent you are new to CouchDB, I'll also give you some advice regarding views. First, the view you've created is actually just a duplicate of the system view _all_docs, so you should just use that instead rather than creating your own view. (especially since you've effectively created a duplicate on disk)
However, it is probably pretty likely that as you get further along in your application, you'll be using real views that partition documents differently depending on the query. As such, you should not emit your entire document (ie: doc) in your view function. By doing this, you are effectively duplicating that document on disk, since it will be represented in your database, as well as the view index.
The recommended starting point is to simply leave out the 2nd argument of your emit.
function (doc) {
emit(doc._id);
}
When you query the view, you can simply add include_docs=true to the URL and your view will look something like this:
[
{
"id": "8767d3474a0e80dd0ab7d0b0580065af",
"key": "8767d3474a0e80dd0ab7d0b0580065af",
"value": null,
"doc": {
"_id": "8767d3474a0e80dd0ab7d0b0580065af",
"_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
"type": "accounts",
"lastname": "Kitchen",
"firstname": "Peter"
}
}
// ...
]
Then, you can retrieve the doc instead of value to achieve the same result much more efficiently.

Mongo DB issue with findOne query

I am maintaining a rooms table, where it consists of records associated with the conversations. I want to get the room id between two users so using findOne query but it's bringing other records and not satisfying my need.
Suggest me where the query has gone wrong.
If I give the query:
rooms.findOne({ "userId" :"800", "userId" :"600"});
I am expectng conversation id of fsny11z742kpgb9 but it's giving 6puebew70kke29.
{
"_id": ObjectId("571c5724db62826826d28d08"),
"conversationId": "6puebew70kke29",
"userId": "600",
"firstName": "Test",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.753Z"),
"__v": 0
}
{
"_id": ObjectId("571c5724db62826826d28d09"),
"conversationId": "6puebew70kke29",
"userId": "900",
"firstName": "User",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.754Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0b"),
"conversationId": "fsny11z742kpgb9",
"userId": "600",
"firstName": "FitTest",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.192Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0c"),
"conversationId": "fsny11z742kpgb9",
"userId": "800",
"firstName": "Dev",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.193Z"),
"__v": 0
}
You have to use aggregation to do so.
rooms.aggregate([
{ $group: { _id: '$conversationId', users: { $push: '$userId' } } },
{ $match: { users: { $all: ['800', '600'] }, groupType: 'PRIVATE' } },
])
The findOne() operation returns the first document according to the natural order which reflects the order of documents on the disk, see mongodb docs.
Second, the query document you provide as parameter to the findOne() operation contains two values for userId, this is not the same as the $in operator. The latter one overrides the first one.
As Mathieu suggested, a proper lookup would be to use an aggregation pipeline with two steps:
rooms.aggregate([
{ $group: { _id: '$conversationId', users: { $push: '$userId' } } },
{ $match: { users: { $all: ['800', '600'] }, groupType: 'PRIVATE' } },
])
create list with id matching the conversation id and a field of type array containing all the userIds ($group stage)
filter out all entries, where the user-array contains the ids of the both users your are looking for. ($match stage)
Bear in mind, that this will return all conversations of both users.