Multi-referencing one collection to another

542 views
Skip to first unread message

Monica at IBM

unread,
Sep 19, 2018, 5:46:43 PM9/19/18
to mongodb-user
Hi everyone, I have 2 collections: person and meeting. I would like to store the person id in the meeting.attendee field. As there will be more than one attendee, I've created an array.  But for some reason I am not able to store the person.ObjectiId("xxxxx") s for each person in the attendee array element. (Did I mention I'm still a rookie?). Should I set up this differently (not an array but something else? what?)  Thanks for any help!!

Robert Cochran

unread,
Sep 19, 2018, 5:58:15 PM9/19/18
to mongodb-user
Hi!

Could you let us know:

The operating systems and operating system versions of both the client and server devices;
The mongoldb version you are using;
Post sample data for both the person and meeting collections;
Share your code for doing the updates that you are having trouble with. Are you using the mongo shell? Or one of the language drivers?

Thanks

Bob

Monica at IBM

unread,
Sep 19, 2018, 6:10:38 PM9/19/18
to mongod...@googlegroups.com
Well, it's Windows 10 and I'm using Compass, just trying it out directly on the database.
This is the setup:
PERSON
_id: ObjectId
Lastname: string
Firstname: string
Etc.. 
MEETING
_id: ObjectId
Date: date
Location:string
Attendees: array
Etc... 

So i intend to put the _id s from Person in the meeting attendees array.
Possible?
Thank you. 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/X9UltGPVpnw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/e8108724-87ff-4ebf-a56e-47ea3868687b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Cochran

unread,
Sep 19, 2018, 9:14:57 PM9/19/18
to mongodb-user
Hi Monica,

Okay. I simulated what you want, using two existing collections which are convenient for my purposes. I think I have a very rough solution to what you want, but it needs work. I don't know what language driver you are using so I worked with the mongo shell. I'm using MongoDB Enterprise version 4.0.2.

Collection "articles" has a short list of documents and the _id field for each document is the standard ObjectId field. Here is are example documents:

MongoDB Enterprise > db.articles.find({})

{ "_id" : ObjectId("5ba13b23a8473592bc32933c"), "author" : "dave", "score" : 80, "views" : 100 }

{ "_id" : ObjectId("5ba13b23a8473592bc32933d"), "author" : "dave", "score" : 85, "views" : 521 }


Collection "mapinput" has 3 documents, and the _id fields are just integer values. Each document has an array called "quizzes". What I did was: extract the ObjectId for each document in articles, and add it to an array in the map input document for _id=2. The new array is named "attendees".

Here is the result for _id=2:

{

"_id" : 2,

"quizzes" : [ ],

"attendees" : [

ObjectId("5ba13b23a8473592bc32933c"),

ObjectId("5ba13b23a8473592bc32933d"),

ObjectId("5ba13b23a8473592bc32933e"),

ObjectId("5ba13b23a8473592bc32933f"),

ObjectId("5ba13b23a8473592bc329340"),

ObjectId("5ba13b23a8473592bc329341"),

ObjectId("5ba13b23a8473592bc329342")

]

}


I did this using Javascript in the mongo shell with this code:

$ cat test_function2.js

use test1

var myIds = new Array()

db.articles.find().forEach(

  function(e) {

    myIds.push(e._id.str)

    var t = e._id.str

    print("user : " + t + " author : " + e.author )

    db.mapinput.update( { "_id" : 2 }, { "$addToSet" : { "attendees" : ObjectId(t)  } } )

  }

)

myIds


Please ignore the values in the javascript array myIds. My experiments with those failed. I am not very good at all with ObjectId type fields, and have trouble working with them. The above code just illustrates how to make an array of ObjectId's extracted from a different collection. It was a bit of a challenge for me to get this result. 

It is possible to write a script file like the above and then redirect it to the mongo shell. Like this:

mongo < test_function2.js

I think you can open an empty command prompt or power shell window and do exactly the same thing. I do all my work on Ubuntu 18.04 LTS so I'm not too familiar with working on the Windows 10 command line.

One more thing. I messed up my mapinput collection a couple times while experimenting with this solution. That was okay! I had a small text file for those 3 documents, and I could just db.mapinput.drop() the broken collection and then re-create it from the text file. That let me conveniently get rid of my mistakes and try again. You might want to mongodump your collections so you can save them and re-import them later if needed. 

I hope it helps you out. 

Bob

Monica at IBM

unread,
Sep 20, 2018, 10:56:03 AM9/20/18
to mongodb-user
Hi Robert, Thank you so very very much!!! Just seeing that it worked for you helped! I was able to insert a object id into the array too. Big thanks!!! M.
Reply all
Reply to author
Forward
0 new messages