MongoDB DBRefs to query reference collection field - node.js

I have two different collection in two different MongoDB databases.
Collection users in db1:
{
"_id" : "xyz",
"name" : "John",
"project_id" : "abc"
}
Collection project in db2:
{
"_id" : "abc",
"regionCode" : "1AB"
}
Now I want to get list of all users who belong to region 1AB. How do I achieve that.
I read about DBRefs. So I changed structure of collections users but still i can not query directly to reference collection attribute directly
collection users new structure :
{
"_id" : "xyz",
"name" : "John",
"project_id" : "abc",
"projectData" : {
"$ref" : "project",
"$id" : "abc",
"$db" : "db2"
}
}
Now how do I get list of all users that belong to project regioncode 1AB.
I am using nodejs native client

Related

how to get the another collection data whose id is stored in array

I am having a collection like this whose users ids are stored inside the array,I need to query the collection A to get collection B whose "_id" (only "_id") is stored in array type , how to get in mongodb using aggregate ?
collection A :
{
"_id" : ObjectId("5ccfc2d176013e48a4ec0eed"),
"position" : ObjectId("5c73bcd0697b545c329a4bd4"),
"users" : [
"5bbf3cc7381e7a6530872089"
],
"inCase" : [],
"completed" : false,
}
am having second collection as
Collection B
{
"_id" : ObjectId("5b517a2bebb3b14b4cd78ca9"),
"firstName" : "Katie",
"lastName" : "Carter",
"languages" : [
"English",
"",
""
],
}
My code
{$lookup:{from:'user',localField:'users',foreignField:'_id',as:'users'}},
{$unwind:'$users'},
let me know how to resolve it?

Querying a property that is in a deeply nested array

So I have this document within the course collection
{
"_id" : ObjectId("53580ff62e868947708073a9"),
"startDate" : ISODate("2014-04-23T19:08:32.401Z"),
"scoreId" : ObjectId("531f28fd495c533e5eaeb00b"),
"rewardId" : null,
"type" : "certificationCourse",
"description" : "This is a description",
"name" : "testingAutoSteps1",
"authorId" : ObjectId("532a121e518cf5402d5dc276"),
"steps" : [
{
"name" : "This is a step",
"description" : "This is a description",
"action" : "submitCategory",
"value" : "532368bc2ab8b9182716f339",
"statusId" : ObjectId("5357e26be86f746b68482c8a"),
"_id" : ObjectId("53580ff62e868947708073ac"),
"required" : true,
"quantity" : 1,
"userId" : [
ObjectId("53554b56e3a1e1dc17db903f")
]
},...
And I want to do is create a query that returns all courses that have a specific userId in the userId array that is in the steps array for a specific userId. I've tried using $elemMatch like so
Course.find({
"steps": {
"$elemMatch": {
"userId": {
"$elemMatch": "53554b56e3a1e1dc17db903f"
}
}
}
},
But It seems to be returning a empty document.
I think this will work for you, you have the syntax off a bit plus you need to use ObjectId():
db.Course.find({ steps : { $elemMatch: { userId:ObjectId("53554b56e3a1e1dc17db903f")} } })
The $elemMatch usage is not necessary unless you actually have compound sub-documents in that nested array element. And also is not necessary unless the value being referenced could possibly duplicate in another compound document.
Since this is an ObjectId we are talking about, then it's going to be unique, at least within this array. So just use the "dot-notation" form:
Course.find({
"steps.userId": ObjectId("53554b56e3a1e1dc17db903f")
},
Go back and look at the $elemMatch documentation. In this case, the direct "dot-notation" form is all you need

how to query a collection based on value of a field array in mongoose

I have the following user schema
{
"__v" : 26,
"_id" : ObjectId("52b47058fe5e3493a2cf8365"),
"live_sockets" : [
"CEGok0rSz2UtBIHX9DlV",
"s6M45OA0MDBs4aGL9DlW",
"2XiszSuyiPAGr-Ga9ZCN",
"lIFNEeUgXRB6tgvP9ZCO",
"JPtzQOD_52maf6VS9gtb",
"kDL06aXiI8WWlig19gtc",
"75Bt5p6WqgmRcyer9xSm",
"Ge_sKLen32Q91wLW9xSn",
"EpHt3qju34GTZevU_Bsd",
"n0hq0EQAjJOptxdy_qEB",
],
"name" : {
"first" : "Test",
"last" : "User"
},
"notifications_count" : 0,
"role" : 1
}
How can i query a user based on a particular live socket as socket_ids are unique.
If i have a variable called current_socket_id how can i find a user which the socket belongs to..?
db.collection.find({"live_sockets" : {$in : [socketId]}},{"name" : 1,"_id":0})
Will give the name for the socket id

Upsert embedded object in mongoDB

Given this Person collection:
{
"_id" : ObjectId("4f8e95a718bcv9c74da1e6511a"),
"name" : "John",
"hobbies" : [{
"id" : 001,
"name" : "reading",
"location" : "home"
},{
"id" : 002,
"name" : "sport",
"location" : "outside"
}]
}
and these new/edited Hobby objects:
{
"name" : "walking",
"location" : "outside"
}
and
{
"id" : 001,
"name" : "reading",
"location" : "outside"
}
If I know the Person that I want to manage, what is be the best way to upsert embedded objects?
Currently my approach is to find the Person object, make the required modifications to it in my code, and then save it back to the DB. This works. But I'd like to simplify and reduce the number of round trips to the database.

How can i retrieve a subrecord from mongo DB

I have a db that looks like this :
{
"_id" : ObjectId("50525e55467f67da3f8baf06"),
"client" : "client1"
}
{
"_id" : ObjectId("505262f0deed32a758410b1c"),
"client" : "client2"
}
{
"_id" : ObjectId("5052fe0a37083fd981616589"),
"client" : "client3",
"products" : [
{"name" : "product1"},
{"name" : "product2"}
]
}
How can i retrieve the product list of client3 without retrieving the client3 record ?
The output should look like this :
[
{"name" : "product1"},
{"name" : "product2"}
]
I don't think you can completely exclude the client3 record as the products are part of this record, but you can select just the products field like this:
db.<dbname>.find({ 'client' : 'client3' }, { 'products' : 1, '_id' : 0 })
Also - if you want to get just the matching subrecord - see here
http://docs.mongodb.org/manual/reference/operator/projection/positional/
you use the $ operator in the project portion of find to specify the n'th subrecord where that is the first to match your query.

Resources