Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- comments
- { uid:12345, pid:444, comment="blah" }
- { uid:12345, pid:888, comment="asdf" }
- { uid:99999, pid:444, comment="qwer" }
- users
- { uid:12345, name:"john" }
- { uid:99999, name:"mia" }
- {
- $lookup:
- {
- from: <collection to join>,
- localField: <field from the input documents>,
- foreignField: <field from the documents of the "from" collection>,
- as: <output array field>
- }
- }
- db.authors.insert([
- {
- _id: 'a1',
- name: { first: 'orlando', last: 'becerra' },
- age: 27
- },
- {
- _id: 'a2',
- name: { first: 'mayra', last: 'sanchez' },
- age: 21
- }
- ]);
- db.categories.insert([
- {
- _id: 'c1',
- name: 'sci-fi'
- },
- {
- _id: 'c2',
- name: 'romance'
- }
- ]);
- db.books.insert([
- {
- _id: 'b1',
- name: 'Groovy Book',
- category: 'c1',
- authors: ['a1']
- },
- {
- _id: 'b2',
- name: 'Java Book',
- category: 'c2',
- authors: ['a1','a2']
- },
- ]);
- db.lendings.insert([
- {
- _id: 'l1',
- book: 'b1',
- date: new Date('01/01/11'),
- lendingBy: 'jose'
- },
- {
- _id: 'l2',
- book: 'b1',
- date: new Date('02/02/12'),
- lendingBy: 'maria'
- }
- ]);
- db.books.find().forEach(
- function (newBook) {
- newBook.category = db.categories.findOne( { "_id": newBook.category } );
- newBook.lendings = db.lendings.find( { "book": newBook._id } ).toArray();
- newBook.authors = db.authors.find( { "_id": { $in: newBook.authors } } ).toArray();
- db.booksReloaded.insert(newBook);
- }
- );
- db.booksReloaded.find().pretty()
- {
- "_id" : "b1",
- "name" : "Groovy Book",
- "category" : {
- "_id" : "c1",
- "name" : "sci-fi"
- },
- "authors" : [
- {
- "_id" : "a1",
- "name" : {
- "first" : "orlando",
- "last" : "becerra"
- },
- "age" : 27
- }
- ],
- "lendings" : [
- {
- "_id" : "l1",
- "book" : "b1",
- "date" : ISODate("2011-01-01T00:00:00Z"),
- "lendingBy" : "jose"
- },
- {
- "_id" : "l2",
- "book" : "b1",
- "date" : ISODate("2012-02-02T00:00:00Z"),
- "lendingBy" : "maria"
- }
- ]
- }
- {
- "_id" : "b2",
- "name" : "Java Book",
- "category" : {
- "_id" : "c2",
- "name" : "romance"
- },
- "authors" : [
- {
- "_id" : "a1",
- "name" : {
- "first" : "orlando",
- "last" : "becerra"
- },
- "age" : 27
- },
- {
- "_id" : "a2",
- "name" : {
- "first" : "mayra",
- "last" : "sanchez"
- },
- "age" : 21
- }
- ],
- "lendings" : [ ]
- }
- db.users.find().forEach(
- function (object) {
- var commonInBoth=db.comments.findOne({ "uid": object.uid} );
- if (commonInBoth != null) {
- printjson(commonInBoth) ;
- printjson(object) ;
- }else {
- // did not match so we don't care in this case
- }
- });
- db.comments.aggregate({
- $lookup:{
- from:"users",
- localField:"uid",
- foreignField:"uid",
- as:"users_comments"
- }
- })
- db.users.aggregate({
- $lookup:{
- from:"comments",
- localField:"uid",
- foreignField:"uid",
- as:"users_comments"
- }
- })
- SELECT S.* FROM LeftTable S
- LEFT JOIN RightTable R ON S.ID =R.ID AND S.MID =R.MID WHERE R.TIM >0 AND
- S.MOB IS NOT NULL
- db.LeftTable.aggregate([
- # connect all tables
- {"$lookup": {
- "from": "RightTable",
- "localField": "ID",
- "foreignField": "ID",
- "as": "R"
- }},
- {"$unwind": "R"}
- ])
- db.LeftTable.aggregate([
- # connect all tables
- {"$lookup": {
- "from": "RightTable",
- "localField": "ID",
- "foreignField": "ID",
- "as": "R"
- }},
- {"$unwind": "R"},
- # define conditionals + variables
- {"$project": {
- "midEq": {"$eq": ["$MID", "$R.MID"]},
- "ID": 1, "MOB": 1, "MID": 1
- }}
- ])
- db.LeftTable.aggregate([
- # connect all tables
- {"$lookup": {
- "from": "RightTable",
- "localField": "ID",
- "foreignField": "ID",
- "as": "R"
- }},
- {"$unwind": "$R"},
- # define conditionals + variables
- {"$project": {
- "midEq": {"$eq": ["$MID", "$R.MID"]},
- "ID": 1, "MOB": 1, "MID": 1
- }},
- # join all conditionals
- {"$match": {
- "$and": [
- {"R.TIM": {"$gt": 0}},
- {"MOB": {"$exists": True}},
- {"midEq": {"$eq": True}}
- ]}},
- # undefine conditionals
- {"$project": {
- "midEq": 0
- }}
- ])
- {
- $lookup:
- {
- from: <collection to join>,
- localField: <field from the input documents>,
- foreignField: <field from the documents of the "from" collection>,
- as: <output array field>
- }
- }
- SELECT *, <output array field>
- FROM collection
- WHERE <output array field> IN (SELECT <documents as determined from the pipeline>
- FROM <collection to join>
- WHERE <pipeline> );
- db.getCollection('comments').aggregate([
- {$match : {pid : 444}},
- {$lookup: {from: "users",localField: "uid",foreignField: "uid",as: "userData"}},
- ])
- var Join = require('mongo-join').Join, mongodb = require('mongodb'), Db = mongodb.Db, Server = mongodb.Server;
- db.open(function (err, Database) {
- Database.collection('Appoint', function (err, Appoints) {
- /* we can put conditions just on the top level */
- Appoints.find({_id_Doctor: id_doctor ,full_date :{ $gte: start_date },
- full_date :{ $lte: end_date }}, function (err, cursor) {
- var join = new Join(Database).on({
- field: '_id_Doctor', // <- field in Appoints document
- to: '_id', // <- field in User doc. treated as ObjectID automatically.
- from: 'User' // <- collection name for User doc
- }).on({
- field: '_id_Patient', // <- field in Appoints doc
- to: '_id', // <- field in User doc. treated as ObjectID automatically.
- from: 'User' // <- collection name for User doc
- })
- join.toArray(cursor, function (err, joinedDocs) {
- /* do what ever you want here */
- /* you can fetch the table and apply your own conditions */
- .....
- .....
- .....
- resp.status(200);
- resp.json({
- "status": 200,
- "message": "success",
- "Appoints_Range": joinedDocs,
- });
- return resp;
- });
- });
- 1) Select from the collection you're interested in.
- 2) From that collection pull out ID's you need
- 3) Select from other collections
- 4) Decorate your original results.
- db.actors.insert( { actor: "Richard Gere", movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] });
- db.actors.insert( { actor: "Julia Roberts", movies: ['Pretty Woman', 'Runaway Bride', 'Erin Brockovich'] });
- map = function() {
- for(var i in this.movies){
- key = { movie: this.movies[i] };
- value = { actors: [ this.actor ] };
- emit(key, value);
- }
- }
- reduce = function(key, values) {
- actor_list = { actors: [] };
- for(var i in values) {
- actor_list.actors = values[i].actors.concat(actor_list.actors);
- }
- return actor_list;
- }
- { "_id" : { "movie" : "Chicago" }, "value" : { "actors" : [ "Richard Gere" ] } }
- { "_id" : { "movie" : "Erin Brockovich" }, "value" : { "actors" : [ "Julia Roberts" ] } }
- { "_id" : { "movie" : "Pretty Woman" }, "value" : { "actors" : [ "Richard Gere", "Julia Roberts" ] } }
- { "_id" : { "movie" : "Runaway Bride" }, "value" : { "actors" : [ "Richard Gere", "Julia Roberts" ] } }
- `db.commentss.insert([
- { uid:12345, pid:444, comment:"blah" },
- { uid:12345, pid:888, comment:"asdf" },
- { uid:99999, pid:444, comment:"qwer" }])`
- db.userss.insert([
- { uid:12345, name:"john" },
- { uid:99999, name:"mia" }])
- `db.commentss.find().forEach(
- function (newComments) {
- newComments.userss = db.userss.find( { "uid": newComments.uid } ).toArray();
- db.newCommentUsers.insert(newComments);
- }
- );`
- db.newCommentUsers.find().pretty()
- `{
- "_id" : ObjectId("5511236e29709afa03f226ef"),
- "uid" : 12345,
- "pid" : 444,
- "comment" : "blah",
- "userss" : [
- {
- "_id" : ObjectId("5511238129709afa03f226f2"),
- "uid" : 12345,
- "name" : "john"
- }
- ]
- }
- {
- "_id" : ObjectId("5511236e29709afa03f226f0"),
- "uid" : 12345,
- "pid" : 888,
- "comment" : "asdf",
- "userss" : [
- {
- "_id" : ObjectId("5511238129709afa03f226f2"),
- "uid" : 12345,
- "name" : "john"
- }
- ]
- }
- {
- "_id" : ObjectId("5511236e29709afa03f226f1"),
- "uid" : 99999,
- "pid" : 444,
- "comment" : "qwer",
- "userss" : [
- {
- "_id" : ObjectId("5511238129709afa03f226f3"),
- "uid" : 99999,
- "name" : "mia"
- }
- ]
- }`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement