Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. comments
  2. { uid:12345, pid:444, comment="blah" }
  3. { uid:12345, pid:888, comment="asdf" }
  4. { uid:99999, pid:444, comment="qwer" }
  5.  
  6. users
  7. { uid:12345, name:"john" }
  8. { uid:99999, name:"mia" }
  9.  
  10. {
  11. $lookup:
  12. {
  13. from: <collection to join>,
  14. localField: <field from the input documents>,
  15. foreignField: <field from the documents of the "from" collection>,
  16. as: <output array field>
  17. }
  18. }
  19.  
  20. db.authors.insert([
  21. {
  22. _id: 'a1',
  23. name: { first: 'orlando', last: 'becerra' },
  24. age: 27
  25. },
  26. {
  27. _id: 'a2',
  28. name: { first: 'mayra', last: 'sanchez' },
  29. age: 21
  30. }
  31. ]);
  32.  
  33. db.categories.insert([
  34. {
  35. _id: 'c1',
  36. name: 'sci-fi'
  37. },
  38. {
  39. _id: 'c2',
  40. name: 'romance'
  41. }
  42. ]);
  43.  
  44. db.books.insert([
  45. {
  46. _id: 'b1',
  47. name: 'Groovy Book',
  48. category: 'c1',
  49. authors: ['a1']
  50. },
  51. {
  52. _id: 'b2',
  53. name: 'Java Book',
  54. category: 'c2',
  55. authors: ['a1','a2']
  56. },
  57. ]);
  58.  
  59. db.lendings.insert([
  60. {
  61. _id: 'l1',
  62. book: 'b1',
  63. date: new Date('01/01/11'),
  64. lendingBy: 'jose'
  65. },
  66. {
  67. _id: 'l2',
  68. book: 'b1',
  69. date: new Date('02/02/12'),
  70. lendingBy: 'maria'
  71. }
  72. ]);
  73.  
  74. db.books.find().forEach(
  75. function (newBook) {
  76. newBook.category = db.categories.findOne( { "_id": newBook.category } );
  77. newBook.lendings = db.lendings.find( { "book": newBook._id } ).toArray();
  78. newBook.authors = db.authors.find( { "_id": { $in: newBook.authors } } ).toArray();
  79. db.booksReloaded.insert(newBook);
  80. }
  81. );
  82.  
  83. db.booksReloaded.find().pretty()
  84.  
  85. {
  86. "_id" : "b1",
  87. "name" : "Groovy Book",
  88. "category" : {
  89. "_id" : "c1",
  90. "name" : "sci-fi"
  91. },
  92. "authors" : [
  93. {
  94. "_id" : "a1",
  95. "name" : {
  96. "first" : "orlando",
  97. "last" : "becerra"
  98. },
  99. "age" : 27
  100. }
  101. ],
  102. "lendings" : [
  103. {
  104. "_id" : "l1",
  105. "book" : "b1",
  106. "date" : ISODate("2011-01-01T00:00:00Z"),
  107. "lendingBy" : "jose"
  108. },
  109. {
  110. "_id" : "l2",
  111. "book" : "b1",
  112. "date" : ISODate("2012-02-02T00:00:00Z"),
  113. "lendingBy" : "maria"
  114. }
  115. ]
  116. }
  117. {
  118. "_id" : "b2",
  119. "name" : "Java Book",
  120. "category" : {
  121. "_id" : "c2",
  122. "name" : "romance"
  123. },
  124. "authors" : [
  125. {
  126. "_id" : "a1",
  127. "name" : {
  128. "first" : "orlando",
  129. "last" : "becerra"
  130. },
  131. "age" : 27
  132. },
  133. {
  134. "_id" : "a2",
  135. "name" : {
  136. "first" : "mayra",
  137. "last" : "sanchez"
  138. },
  139. "age" : 21
  140. }
  141. ],
  142. "lendings" : [ ]
  143. }
  144.  
  145. db.users.find().forEach(
  146. function (object) {
  147. var commonInBoth=db.comments.findOne({ "uid": object.uid} );
  148. if (commonInBoth != null) {
  149. printjson(commonInBoth) ;
  150. printjson(object) ;
  151. }else {
  152. // did not match so we don't care in this case
  153. }
  154. });
  155.  
  156. db.comments.aggregate({
  157. $lookup:{
  158. from:"users",
  159. localField:"uid",
  160. foreignField:"uid",
  161. as:"users_comments"
  162. }
  163. })
  164.  
  165. db.users.aggregate({
  166. $lookup:{
  167. from:"comments",
  168. localField:"uid",
  169. foreignField:"uid",
  170. as:"users_comments"
  171. }
  172. })
  173.  
  174. SELECT S.* FROM LeftTable S
  175. LEFT JOIN RightTable R ON S.ID =R.ID AND S.MID =R.MID WHERE R.TIM >0 AND
  176. S.MOB IS NOT NULL
  177.  
  178. db.LeftTable.aggregate([
  179. # connect all tables
  180.  
  181. {"$lookup": {
  182. "from": "RightTable",
  183. "localField": "ID",
  184. "foreignField": "ID",
  185. "as": "R"
  186. }},
  187. {"$unwind": "R"}
  188.  
  189. ])
  190.  
  191. db.LeftTable.aggregate([
  192. # connect all tables
  193.  
  194. {"$lookup": {
  195. "from": "RightTable",
  196. "localField": "ID",
  197. "foreignField": "ID",
  198. "as": "R"
  199. }},
  200. {"$unwind": "R"},
  201.  
  202. # define conditionals + variables
  203.  
  204. {"$project": {
  205. "midEq": {"$eq": ["$MID", "$R.MID"]},
  206. "ID": 1, "MOB": 1, "MID": 1
  207. }}
  208. ])
  209.  
  210. db.LeftTable.aggregate([
  211. # connect all tables
  212.  
  213. {"$lookup": {
  214. "from": "RightTable",
  215. "localField": "ID",
  216. "foreignField": "ID",
  217. "as": "R"
  218. }},
  219. {"$unwind": "$R"},
  220.  
  221. # define conditionals + variables
  222.  
  223. {"$project": {
  224. "midEq": {"$eq": ["$MID", "$R.MID"]},
  225. "ID": 1, "MOB": 1, "MID": 1
  226. }},
  227.  
  228. # join all conditionals
  229.  
  230. {"$match": {
  231. "$and": [
  232. {"R.TIM": {"$gt": 0}},
  233. {"MOB": {"$exists": True}},
  234. {"midEq": {"$eq": True}}
  235. ]}},
  236.  
  237. # undefine conditionals
  238.  
  239. {"$project": {
  240. "midEq": 0
  241. }}
  242.  
  243. ])
  244.  
  245. {
  246. $lookup:
  247. {
  248. from: <collection to join>,
  249. localField: <field from the input documents>,
  250. foreignField: <field from the documents of the "from" collection>,
  251. as: <output array field>
  252. }
  253. }
  254.  
  255. SELECT *, <output array field>
  256. FROM collection
  257. WHERE <output array field> IN (SELECT <documents as determined from the pipeline>
  258. FROM <collection to join>
  259. WHERE <pipeline> );
  260.  
  261. db.getCollection('comments').aggregate([
  262. {$match : {pid : 444}},
  263. {$lookup: {from: "users",localField: "uid",foreignField: "uid",as: "userData"}},
  264. ])
  265.  
  266. var Join = require('mongo-join').Join, mongodb = require('mongodb'), Db = mongodb.Db, Server = mongodb.Server;
  267. db.open(function (err, Database) {
  268. Database.collection('Appoint', function (err, Appoints) {
  269.  
  270. /* we can put conditions just on the top level */
  271. Appoints.find({_id_Doctor: id_doctor ,full_date :{ $gte: start_date },
  272. full_date :{ $lte: end_date }}, function (err, cursor) {
  273. var join = new Join(Database).on({
  274. field: '_id_Doctor', // <- field in Appoints document
  275. to: '_id', // <- field in User doc. treated as ObjectID automatically.
  276. from: 'User' // <- collection name for User doc
  277. }).on({
  278. field: '_id_Patient', // <- field in Appoints doc
  279. to: '_id', // <- field in User doc. treated as ObjectID automatically.
  280. from: 'User' // <- collection name for User doc
  281. })
  282. join.toArray(cursor, function (err, joinedDocs) {
  283.  
  284. /* do what ever you want here */
  285. /* you can fetch the table and apply your own conditions */
  286. .....
  287. .....
  288. .....
  289.  
  290.  
  291. resp.status(200);
  292. resp.json({
  293. "status": 200,
  294. "message": "success",
  295. "Appoints_Range": joinedDocs,
  296.  
  297.  
  298. });
  299. return resp;
  300.  
  301.  
  302. });
  303.  
  304. });
  305.  
  306. 1) Select from the collection you're interested in.
  307. 2) From that collection pull out ID's you need
  308. 3) Select from other collections
  309. 4) Decorate your original results.
  310.  
  311. db.actors.insert( { actor: "Richard Gere", movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] });
  312. db.actors.insert( { actor: "Julia Roberts", movies: ['Pretty Woman', 'Runaway Bride', 'Erin Brockovich'] });
  313.  
  314. map = function() {
  315. for(var i in this.movies){
  316. key = { movie: this.movies[i] };
  317. value = { actors: [ this.actor ] };
  318. emit(key, value);
  319. }
  320. }
  321.  
  322. reduce = function(key, values) {
  323. actor_list = { actors: [] };
  324. for(var i in values) {
  325. actor_list.actors = values[i].actors.concat(actor_list.actors);
  326. }
  327. return actor_list;
  328. }
  329.  
  330. { "_id" : { "movie" : "Chicago" }, "value" : { "actors" : [ "Richard Gere" ] } }
  331. { "_id" : { "movie" : "Erin Brockovich" }, "value" : { "actors" : [ "Julia Roberts" ] } }
  332. { "_id" : { "movie" : "Pretty Woman" }, "value" : { "actors" : [ "Richard Gere", "Julia Roberts" ] } }
  333. { "_id" : { "movie" : "Runaway Bride" }, "value" : { "actors" : [ "Richard Gere", "Julia Roberts" ] } }
  334.  
  335. `db.commentss.insert([
  336. { uid:12345, pid:444, comment:"blah" },
  337. { uid:12345, pid:888, comment:"asdf" },
  338. { uid:99999, pid:444, comment:"qwer" }])`
  339.  
  340. db.userss.insert([
  341. { uid:12345, name:"john" },
  342. { uid:99999, name:"mia" }])
  343.  
  344. `db.commentss.find().forEach(
  345. function (newComments) {
  346. newComments.userss = db.userss.find( { "uid": newComments.uid } ).toArray();
  347. db.newCommentUsers.insert(newComments);
  348. }
  349. );`
  350.  
  351. db.newCommentUsers.find().pretty()
  352.  
  353. `{
  354. "_id" : ObjectId("5511236e29709afa03f226ef"),
  355. "uid" : 12345,
  356. "pid" : 444,
  357. "comment" : "blah",
  358. "userss" : [
  359. {
  360. "_id" : ObjectId("5511238129709afa03f226f2"),
  361. "uid" : 12345,
  362. "name" : "john"
  363. }
  364. ]
  365. }
  366. {
  367. "_id" : ObjectId("5511236e29709afa03f226f0"),
  368. "uid" : 12345,
  369. "pid" : 888,
  370. "comment" : "asdf",
  371. "userss" : [
  372. {
  373. "_id" : ObjectId("5511238129709afa03f226f2"),
  374. "uid" : 12345,
  375. "name" : "john"
  376. }
  377. ]
  378. }
  379. {
  380. "_id" : ObjectId("5511236e29709afa03f226f1"),
  381. "uid" : 99999,
  382. "pid" : 444,
  383. "comment" : "qwer",
  384. "userss" : [
  385. {
  386. "_id" : ObjectId("5511238129709afa03f226f3"),
  387. "uid" : 99999,
  388. "name" : "mia"
  389. }
  390. ]
  391. }`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement