MongoDB Merge Equivalent Collections - mongodb

I have two collections with same structure and want to merge them in aggregation result and query & sort over them after merging.
E.g.;
First collection:
{_id: "123", "name": "sercan"}
Second collection:
{_id: "456", "name": "hakan"}
What I want;
[{_id: "123", "name": "sercan"}, {_id: "456", "name": "hakan"}]
What I tried;
{"from":"secondCollection",pipeline: [],"as":"seconds"}
// result
{_id: "123", "name": "sercan", seconds: [{_id: "456", "name": "hakan"}]}
And the above trial, puts all documents into seconds if there're more documents in the second collection.
Thanks in advance

The one option I have used is $merge in MongoDB.
It's basically merge one collection to another and create new output collection if it does not exist.
Reference:
https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/

Related

MongoDB query: all documents that contain reference to a specific id

I am trying to extract some data from a legacy mongo db (v. 2.0.4). I have data that is structured like this:
{"_id": "1",
"#graph": {"ma:isMemberOf": [{"#id": "524224b804743b02a4c23488",
"title": "IHum 350",
"transcript": "False"},
{"#id": "53cfd59404743bc3c9119adf",
"restrictor": "578e89ae04743b7b0816beff",
"title": "Spanish 339",
"transcript": "False"}],
"ma:title": "Toy title 1"},
"_id": "2",
"#graph": {"ma:isMemberOf": [{"#id": "524224b804743b02a4c23488",
"title": "IHum 350",
"transcript": "False"}],
"ma:title": "Toy title 2"}}
...and I want to write a query that will find all of the documents that are members of a particular group (matching the #id field). For example, I want to be able to search for 524224b804743b02a4c23488 and receive documents 1 and 2. Or to search for 53cfd59404743bc3c9119adf and receive only document 1.
I have tried several things, but I can't figure out how to query embedded objects.
db.collection.find({
"#graph.ma:isMemberOf.#id": <your query>
})
Here is the Mongo playground for your reference.

How to connect to collections by nested fields in MongoDB

I am struggling with some query in MongoDB. Let's say I have standings collection which looks like
{
"competitions: {id: "1", name:"someLeague"},
"standings": [
{
"type": "TOTAL",
"table": [
{
"position": "1",
"team": {
"id": "123",
"name": "XYZ"
},
won: "1",
draw: "2",
lost: "3",
points: "4",
},
{
"position": "2",
"team": {
"id": "321",
"name": "ABC"
}
...
And the fixtures collection which looks like
{
matchDay: "YYYY-MM-DD",
homeTeam: {id: "123", name:"ABC"},
awayTeam: {id: "321", name:"XYZ"},
}
Is it possible to connect this two collection this way that field "homeTeam" in fixtures collection will contain all information including points, won games etc. from standings where type would be total? And same thing with the field awayTeam, with the proviso that information of team would be from array where standings type is away.
There is no means in MongoDB to reference a document of collection A in collection B so that find queries on collection B automatically provide attributes of the referenced document. However, as of MongoDB 3.2 it is possible to use $lookup command as part of an aggregation (see https://stackoverflow.com/a/33511166/3976662) to JOIN (similar to standard SQL) over multiple collections during the query. In your case, you can consider using $lookup in conjunction with $unwind - similar to the example in the MongoDB docs. Spring Data Mongo supports $lookup since 1.10.

Mongodb query for arrays of combination two fields

Say I have a document in the database:
{"name": "Jason", "score": 20, "color": "blue"}
And I have an array of data that contains documents with name and score, is there a way for me to query for the combination of the name and score via $in? For example, if I had a list that looked like
var data = [
{"name": "Bob", "score": 12}
{"name": "Jason", "score": 20}
{"name": "Tammy", "score": 19}
];
And I wanted to query the collection to see if any combination of name and score found within data existed within said collection, how could I do that?
Close because $in is actually a shortened form of $or. You already have the array there so:
db.collection.find({ "$or": data })

field within the object of array in Mongodb

The sample structure is as follows:
{
"_id": 1,
"College_name" : "abcdef"
"Students": [
{
"name": "a",
"Grade": "First"
},
{
"name": "b",
"Grade": "Second"
},
{
"name": "c",
"Grade": "First"
},
{
"name": "d",
"Grade": "First"
}
]
}
What I am not getting is - I want to get all ("Grade" : "First") objects in the array and insert only ("Grade" : "First") objects to other collection where ("College_name" : "abcdef"). What is the optimal solution for this?
Is this possible through aggregation??
Please help me out to solve this.
You can fetch the data with {"Grade": "First"} either using aggregation or mongo query -
Aggregation query -
db.coll.aggregate([{'$match':{"Students.Grade":"First"}}, {$project: {"_id":1, "College_name":1}}])
Mongo Query -
db.coll.find({"Students.Grade":"First"}, {"College_name":1})
After that you can insert the data to other collection.
Answer to this question is in the link:
query to retrieve multiple objects in an array in mongodb
i.e.,
Aggregation query will have 4 pipeline operators:
[match, unwind, match, group]
and then can be inserted to the other collection.

Duplicating a Mongoose document with subdocs, duplicated id of subdocs are allowed?

To clarify: I have a document with a subdoc. I create a new document with the same data of the other one and it gets a new id. However, when I copy the subdoc array they do not get a new id.
Are subdocs id local to the parent doc? I.e. would the following be a problem?
[
{
"__v": 1,
"_id": "5214af03a9f53efa61000004",
"name": "Foo",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
},
{
"__v": 0,
"name": "Foo",
"_id": "5214af03a9f53efa61000014",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
}
]
There is a unique index on the _id field of documents stored directly in a collection, but not for embedded documents, nor is there any requirement that embedded documents have an _id field at all. The two documents you have provided are both valid to be stored in MongoDB in the same database (I'm interpreting your example as an array of two documents that are both stored directly in a collection together).