Change document structure in mongodb with the mongo shell [duplicate] - mongodb

This question already has an answer here:
Reshape all the documents in the collection
(1 answer)
Closed 6 years ago.
I have a document structure in mongodb and i want to change it for all my documents without using aggregate function like this response but by creating a specific function, and have a result from that :
{
"_id" : ObjectId("String"),
"content" : {
"href" : "String",
"text" : "String",
"code" : "String "
}
}
to that :
{
"_id" : ObjectId("String"),
"href" : "String",
"text" : "String",
"code" : "String "
}
Any suggestions please. Thank you.

If you do not fancy aggregation (or your content's content may differ across documents) you can also use
db.coll.find().forEach(function (d) {
db.coll.update({
_id : d._id
}, {
$set : d.content,
$unset : {
'content' : 1
}
})
});

The easiets way to do it is to use aggregation framework.
Then save output into newCollection and after verification you can delete old one and rename created one.
db.collection.aggregate([{
$project : {
_id : 1,
href : "$content.href",
text : "$content.text",
code : "$content.code",
}
}, {
$out : "newCollection"
}
])

Related

MongoDb - Query for specific subdocument

I have a set of mongodb documents with the following structure:
{
"_id" : NUUID("58fbb893-dfe9-4f08-a761-5629d889647d"),
"Identifiers" : {
"IdentificationLevel" : 2,
"Identifier" : "extranet\\test#test.com"
},
"Personal" : {
"FirstName" : "Test",
"Surname" : "Test"
},
"Tags" : {
"Entries" : {
"ContactLists" : {
"Values" : {
"0" : {
"Value" : "{292D8695-4936-4865-A413-800960626E6D}",
"DateTime" : ISODate("2015-04-30T09:14:45.549Z")
}
}
}
}
}
}
How can I make a query with the mongo shell which finds all documents with a specific "Value" (e.g.{292D8695-4936-4865-A413-800960626E6D} in the Tag.Entries.ContactLists.Values path?
The structure is unfortunately locked by Sitecore, so it is not an options to use another structure.
As your sample collection structure show Values is object, it contains only one Value. Also you must check for Value as it contains extra paranthesis. If you want to get Value from given structure try following query :
db.collection.find({
"Tags.Entries.ContactLists.Values.0.Value": "{292D8695-4936-4865-A413-800960626E6D}"
})

MongoDB fields limitation in array [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 8 years ago.
I am looking for a way - and dont even now if this is possible - just to return a part of a list saved in mongodb.
Lets have a look in my currently document:
{
_id : 'MyId',
name : 'a string',
conversations : [
{
user : 'Mike',
input : 'Some input'
},
{
user : 'Stephano',
input : 'some other input'
}
]
}
What I now want to do is smth like this:
var myOutput;
myOutput = db.my_collection.find(
{
_id : 'MyId',
'conversations.user' : 'Mike'
}, {
_id : 1,
name : 1,
conversations : {
$where : {
user : 'Mike'
}
}
});
Goal is it just to get back the conversation array item where user has the value Mike.
Is this still possible in MongoDB ? didn't found any reference in the documentation for the field limitations in mongoDB.
Use the $ positional operator in a projection:
> db.my_collection.find({ "_id" : "MyId", "conversations.user" : "Mike" },
{ "_id" : 1, "name" : 1, "conversations.$" : 1 })
{
"_id" : 'MyId',
"name" : 'a string',
"conversations" : [
{ "user" : 'Mike', "input" : 'Some input' }
]
}
This projects only first matching array element.
Are you aware of the aggregation pipeline?
db.my_collection.aggregate([
{ "$match": { "_id": "MyId"}}, { "$unwind": "$conversations"},
{ "$match": {"conversations.user": "Mike"}}
])
Output
{
"_id" : "MyId",
"name" : "a string",
"conversations" :
{
"user" : "Mike",
"input" : "Some input"
}
}

Get specific object in array of array in MongoDB

I need get a specific object in array of array in MongoDB.
I need get only the task object = [_id = ObjectId("543429a2cb38b1d83c3ff2c2")].
My document (projects):
{
"_id" : ObjectId("543428c2cb38b1d83c3ff2bd"),
"name" : "new project",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b"),
"members" : [
ObjectId("5424ac37eb0ea85d4c921f8b")
],
"US" : [
{
"_id" : ObjectId("5434297fcb38b1d83c3ff2c0"),
"name" : "Test Story",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b"),
"tasks" : [
{
"_id" : ObjectId("54342987cb38b1d83c3ff2c1"),
"name" : "teste3",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
},
{
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
]
}
]
}
Result expected:
{
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
But i not getting it.
I still testing with no success:
db.projects.find({
"US.tasks._id" : ObjectId("543429a2cb38b1d83c3ff2c2")
}, { "US.tasks.$" : 1 })
I tryed with $elemMatch too, but return nothing.
db.projects.find({
"US" : {
"tasks" : {
$elemMatch : {
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2")
}
}
}
})
Can i get ONLY my result expected using find()? If not, what and how use?
Thanks!
You will need an aggregation for that:
db.projects.aggregate([{$unwind:"$US"},
{$unwind:"$US.tasks"},
{$match:{"US.tasks._id":ObjectId("543429a2cb38b1d83c3ff2c2")}},
{$project:{_id:0,"task":"$US.tasks"}}])
should return
{ task : {
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
Explanation:
$unwind creates a new (virtual) document for each array element
$match is the query part of your find
$project is similar as to project part in find i.e. it specifies the fields you want to get in the results
You might want to add a second $match before the $unwind if you know the document you are searching (look at performance metrics).
Edit: added a second $unwind since US is an array.
Don't know what you are doing (so realy can't tell and just sugesting) but you might want to examine if your schema (and mongodb) is ideal for your task because the document looks just like denormalized relational data probably a relational database would be better for you.

MongoDB - How to find where a document field contains a subdocument with a given name? (Example query and document included)

Here I am trying to retrieve documents where the document contains a document DummyFoo, which in turn contains a document DummyBar?
As far as I can tell my syntax below is correct, but no results are returned, though I know that they exist. Have I structured my query incorrectly?
{ "DummyFoo" : { "$elemMatch" : { "DummyBar": { "$exists" : true } }}}
Below is an example of a document that I would expect to match:
{
"_id" : ObjectId("5423d66ba077e6205cc9ecef"),
"Title" : "Test 1234587611",
"Subtitle" : "Test 1 Subtitle",
"DummyFoo" : {
"DummyDate" : "2014-09-25T09:46:35.6626303+01:00",
"DummyBar" : {
"Name" : "Test",
"Number" : 1
}
}
}
$elemMatch is specifically made for querying arrays.In this case, you have a nested json, which mongodb recognises as an embedded document.
You can use the dot notation to query embedded documents.
{ "DummyFoo.DummyBar": { "$exists" : true }}

Change _id value from a number to ObjectId()? [duplicate]

This question already has answers here:
How to update the _id of one MongoDB Document?
(7 answers)
Closed 8 years ago.
I have a collection (510 documents) where _id are numbers :
{ "_id" : "1",
"name" : "eric" }
{ "_id" : "2",
"name" : "tom" }
....
and I want to change their values to ObjectId() to be like this:
{ "_id" : ObjectId("53849e258bf3be07804a00d0"),
"name" : "eric" }
{ "_id" : ObjectId("53849e388bf3be07804a00d1"),
"name" : "tom" }
....
so I tried:
db.contacts.find().forEach(function(doc) {
db.contacts.update(
{ "_id": doc._id },
{ "$set": { "_id": ObjectId()} }
);
})
but it doesn't work, I got like result:
Mod on _id not allowed
Thank you.
You cannot change the _id of an Object in a collection once it is created (it is not allowed by mongodb at all). However you could copy the full content of the object execpt of the _id and add your new _id with the ObjectId and insert it into your collection. Afterwards you can remove all the Objects where the _id is a number. It's a bit more complicated but the result is the same (and it works).
Have a look here.