0

i want to read the document from mongo DB which is having same key but different value.

suppose there are documents having scenario field but different value

ex - document 1 having scenario : 'something1', document2 having scenario : 'something2'...

Sample O/P

 {{_id=6114ce6514796e5fbe316be2, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$25.16, AQ perDayNetPrice=, Test #=WH POS 2.A, Scenario=WH POS 2.A}} 

{{_id=6114ce6514796e5fbe316bde, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$26.51, AQ perDayNetPrice=, Gold "Upgrade Tax"=, Gold Source=, , Scenario=WH HERA}}
johan jk
  • 39
  • 1
  • 6
  • Are you asking how to find all of the documents that contain a certain field? – Montgomery Watts Aug 13 '21 at 02:41
  • yes , like i want to filter all the documents that have scenario field (which is common in all document but diffrent value ). – johan jk Aug 13 '21 at 02:44
  • Does this answer your question? [Check that Field Exists with MongoDB](https://stackoverflow.com/questions/19868016/check-that-field-exists-with-mongodb) – Montgomery Watts Aug 13 '21 at 02:45
  • _id : 6114ce6514796e5fbe316bda App : : "" : "" SnApp Ticket Value : "" SnApp Ticket Tax : "" AQ netPrice : "" Status : "Complete" Execution Environment : "STAGE" Order ID : "VGVXVKRD" Gold "Net Price to Seller" : "" Test # : "WH HERA 1.F" Scenario : "WH HERA 1.F"} _id : 6114ce6614796e5fbe316be4 App : SnApp Ticket Value : "" SnApp Ticket Tax : "" AQ netPrice : "" Status : "Complete" Execution Environment : "STAGE" Order ID : "KJWT62312828" Gold "Net Price to Seller" : "" Test # : "WH HERA 2.A" Scenario : "WH HERA 2.A"} – johan jk Aug 13 '21 at 02:51
  • How to fetch where scenarion is common but diffrent value – johan jk Aug 13 '21 at 02:52
  • What do you consider to be common? – Montgomery Watts Aug 13 '21 at 02:58
  • if you see the data i have given there 'Scenario ' field is common in each documents but diffrent value , so i want to write a querry inside a function which can give the document as per my Scenario value input . And – johan jk Aug 13 '21 at 03:02
  • I can see they start with similar letters. Is that what you want, to find all documents with a scenario that starts with `WH HERA`? – Montgomery Watts Aug 13 '21 at 03:24
  • scenario value will be hard coded ., i.e if i am passing 3 scenario value then i should get the document as per my I/p . like if i am passing scenario = 'A',''B ' then the document having scenario value 'A' and 'B ' should display in console – johan jk Aug 13 '21 at 03:30
  • Are you trying to do exact matches? For example, match all documents that have a scenario equal to "scenario1" OR "scenario2". Or are you trying to match documents that have a scenario that starts with a given input? – Montgomery Watts Aug 13 '21 at 03:35
  • match all documents that have a scenario equal to "scenario1" AND "scenario2" – johan jk Aug 13 '21 at 03:36
  • Do you mean something like [this?](https://docs.mongodb.com/manual/reference/operator/query/in/#use-the--in-operator-to-match-values) – Montgomery Watts Aug 13 '21 at 03:40
  • Data fetch::Document{{_id=6114ce6514796e5fbe316be2, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$25.16, AQ perDayNetPrice=, Test #=WH POS 2.A, Scenario=WH POS 2.A Data fetch::Document{{_id=6114ce6514796e5fbe316bde, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$26.51, AQ perDayNetPrice=, Gold "Upgrade Tax"=, Gold Source=, , Scenario=WH HERA – johan jk Aug 13 '21 at 03:45
  • Please edit your question to include sample documents, how you want to query the sample documents, and which documents should be returned. – Montgomery Watts Aug 13 '21 at 03:59
  • Sample O/P {{_id=6114ce6514796e5fbe316be2, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$25.16, AQ perDayNetPrice=, Test #=WH POS 2.A, Scenario=WH POS 2.A}} {{_id=6114ce6514796e5fbe316bde, App=PRO_WH_DS, Other Voucher Code=, Date and Time Performed=, SnApp Error=, Done=, Unit Tax Quoted=$26.51, AQ perDayNetPrice=, Gold "Upgrade Tax"=, Gold Source=, , Scenario=WH HERA}} – johan jk Aug 13 '21 at 04:01
  • Where Scenario=WH POS 2.A, Scenario=WH HERA and the values (WH POS 2.A, WH HERA ) will pass from a function and it should get display in the console – johan jk Aug 13 '21 at 04:02
  • Please include information like this in the question, *not* the comments. It looks like you want something like the $in operator I linked to. Did you look at it? – Montgomery Watts Aug 13 '21 at 04:05
  • @MontgomeryWatts as per the link written quarry async function findListingScenarion (client, { nameoflist1 =' ', nameoflist2 = '' } ) { const cursor =client.db ('snapp_transactions').collection ('price_check_validation').find ({ Scenario: { $eq: nameoflist1, nameoflist2 } }); – johan jk Aug 13 '21 at 04:15
  • and calling that inside a main() await findListingScenarion (client, nameoflist1 = 'WH HERA 2.A', nameoflist1 ='WH HERA 2.B') – johan jk Aug 13 '21 at 04:16

1 Answers1

0

From our discussion in the comments, it sounds like you want to match documents where the value of Scenario is equal to one of two values. { Scenario: { $eq: nameoflist1, nameoflist2 } } won't work because it isn't a valid filter. This is when you'd want to use the $in operator.

The section I linked in the comments shows a scenario just like yours. Following along, we would reach a query like this:

db.price_check_validation.find( { Scenario: { $in: ['WH HERA 2.A', 'WH HERA 2.B'] } } )
Montgomery Watts
  • 3,806
  • 2
  • 10
  • 24
  • yes , correct as per the suggstion wtitten the querry as below async function finddata (client, l1, l2) { const cursor =client.db ('snapp_transactions').collection ('price_check_validation').find ({ Scenario: { $in: [ 'l1', 'l2' ] } }); } And the value i am passing fro the another function await finddata (client, 'WH HERA 2.A', 'WH HERA 1.F') ; not sure getting error , is my approach is correct ? – johan jk Aug 13 '21 at 04:54
  • Your approach is not correct because you want the values of the variables named `l1` and `l2` but you are passing two strings with the values `"l1"` and `"l2"`. You need to remove the quotes around `l1` and `l2` in the filter document. – Montgomery Watts Aug 13 '21 at 04:57
  • async function finddata (client, l1, l2) { const cursor =client.db ('snapp_transactions').collection ('price_check_validation').find ({ Scenario: { $in: [ l1, l2 ] } }); } but it displaying error - { Error: Cannot find module '../build/Release/bson' at Function.Module._resolveFilename (module.js:548:15) at Function.Module._load (module.js:475:25) at Module.require (module.js:597:17) at require (internal/module.js:11:18) – johan jk Aug 13 '21 at 05:08
  • That appears to be a separate issue related to the NPM package mongoose. Searching that error brings up other stack overflow questions. The answers [here](https://stackoverflow.com/questions/28651028/cannot-find-module-build-release-bson-code-module-not-found-js-bson) might help you. – Montgomery Watts Aug 13 '21 at 05:18
  • yah , i have change the path to bson = require('../browser_build/bson'); but its not showing any O/p – johan jk Aug 13 '21 at 05:24