With this dataset, I’m trying to do a query which return for all the document, the rank of the articles relevant. But I don’t know if it is possible with only a mongo query.
content_business : {
id : {….} ,
content : {
uid : « 01234 »,
FL : "bla",
langRef : 1,
articles : [
{
name : « aName »,
rank : 104
},
{
name : « unNom »,
rank : 102
}
]
}
}
A content contains a langRef. This is the index to use to get the right article. Here with the value 1 it means that the articles which is relevant is the one with the index 1 { name : « unNom », rank : 102 }.
For the moment I do a db.find({« FL : bla »}), then with an external program I’m getting the rank of the related (sort of articles[langRef].rank)
But not sure it is the better solution.
Have you got any idea ?
Regards,
Blured.
It is not really a sort as far as I understand :
What I have got in entry :
content_business : {
id : {….} ,
content : {
uid : « 01234 »,
FL : "bla",
langRef : 1,
articles : [
{
name : « aName »,
rank : 104
},
{
name : « unNom »,
rank : 102
}
]
}
},
{
id : {….} ,
content : {
uid : « 99999 »,
FL : "bla",
langRef : 0,
articles : [
{
name : « aaaa »,
rank : 888
},
{
name : « bbbb »,
rank : 102
}
]
}
}
What I'd like in result
{
id : {...},
content : { name : "unNom", rank : 102}
},
{
id : {...},
content : {name : "aaa", rank : 888}
}
The first document give the name unNom & rank 102 as the specified langRef is 1
the second document give the name "aaa" & rank 888 as the specified langRef is 0
Regards,
Blured.
Related
I have a collection which has a sample object as follows :
{
name : 'Sachin Tendulkar',
followers : {
location : {
countries : [{
name : 'India',
count : 12345
}, {
name : 'Pakistan',
count : 12345
},{
name : 'Australia',
count : 12345
}],
cities : [{
name : 'Mumbai',
count : 12345
},{
name : 'Karachi',
count : 12345
},{
name : 'Melborne',
count : 12345
}]
states : [{
name : 'Maharastra',
count : 12345
},{
name : 'Balochistan',
count : 12345
},{
name : 'Sydney',
count : 12345
}]
}
}
}
I wish to sort all the documents based on the city count. For example,
Sort all documents according to a specific city i.e. Mumbai's count
Can you help me build a query for sorting document as per the conditions mentioned above ?
I think this will be the query:
db.collection_name.aggregate([
{$unwind:"$followers.location.cities"},
{$match:{followers.location.cities.name:"Mumbai"},
{$sort:{"followers.location.cities.count":1}}
])
well... I don't know anything about not relational data base and I wanna know if the following script is correct.
The data base it's developed for obtain management of users and schedules, (The schedules are related with a professor and a classroom). The classrooms have a code and they can be commented and rated by users.
db.user.insert({
//Can be 6 o 10 digits
_id : 201309,
name : {
First : "Fernando",
Last : "Sarmiento"
},
area : 2,
account : {
email : "email#google.com",
password : "Password1!",
level : 0
},
status : {
status : "Active",
Date : 17-01-2016
},
//Can be null
comment : "I like cereal",
})
-
db.schedule.insert({
_id: ObjectId(),
day : "Lunes",
hour : 13 : 00,
finalHour : 14 : 00,
teacher : {
$ref : "user",
$id : $id.user
},
classroom : {
$ref : "classroom",
$id : $id.classrom
}
})
-
db.classroom.insert({
_id : ObjectId(),
name : "Software Development",
comment : "classrom 3",
//Can take values from 0 to 5
score : $avg(userScore),
code : [
{
code : "ABCDEF123456",
user : [{
$ref : "user",
$id : $id.user
}]
}
],
post : [
{
post : "I like the classrom :)",
date : 17-01-2016,
user : [{
$ref : "user",
$id : $id.user
}]
}
],
userScore : [{
score : 4.5,
user : [{
$ref : "user",
$id : $id.user
}]
}]
})
Thanks! :3
I have collection with documents like this
[
{ _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2] },
{ _id : ObjectId("yyyyyy") , u_id : 4 , name : "D" , comment : [] },
{ _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] },
{ _id : ObjectId("aaaaaa") , u_id : 2 , name : "B" , comment : [1,2] },
{ _id : ObjectId("bbbbbb") , u_id : 1 , name : "A" , comment : [1] },
]
Now I have an array of documents prepare to Insert or Update to this collection like this
var multi_document =
[
{ u_id : 8 , name : "H" , comment : [1,2] }, //Insert new document
{ u_id : 7 , name : "G" , comment : [] }, //Insert new document
{ u_id : 6 , name : "F" , comment : [1,2] }, //Insert new document
{ u_id : 5 , name : "E" , comment : [1,2,3] }, //update [1,2] to [1,2,3]
{ u_id : 4 , name : "DD" , comment : [1] }, //update D to DD and [] to [1]
{ u_id : 3 , name : "C" , comment : [1,2] }, //not do anything it same original
];
Can I use db.collection.update(multi_document); ? If not , What should I do?
This is the expected result:
[
{ _id : ObjectId("db_created") , u_id : 8 , name : "H" , comment : [1,2] },
{ _id : ObjectId("db_created") , u_id : 7 , name : "G" , comment : [] },
{ _id : ObjectId("db_created") , u_id : 6 , name : "F" , comment : [1,2] },
{ _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2,3] },
{ _id : ObjectId("yyyyyy") , u_id : 4 , name : "DD" , comment : [1] },
{ _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] },
{ _id : ObjectId("aaaaaa") , u_id : 2 , name : "B" , comment : [1,2] },
{ _id : ObjectId("bbbbbb") , u_id : 1 , name : "A" , comment : [1] },
]
The best way to do this is using the "Bulk" API.
First you need to loop over your multi_document array and for each document find in your collection, documents with same u_id. To that we need to use the bulk.find.upsert method which sets the upsert to true and then use the .update method which specifies the fields to update, here comment. In the update document you need to use the $addToSet operator to ensure that there are not duplicate in your comment field and the $each modifier because comment is an array.
var bulk = db.collection.initializeOrderedBulkOp(),
count = 0;
multi_document.forEach( function (doc) {
bulk.find({ "u_id": doc.u_id })
.upsert()
.update({
"$set": { "name": doc.name },
"$addToSet": { "comment": { "$each": doc.comment }}
});
count++;
if ( count % 1000 == 0 ) {
// Execute per 1000 operations and re-init.
bulk.execute();
bulk = db.collection.initializeOrderedBulkOp();
}
})
// Clean up queues
if ( count % 1000 != 0 )
bulk.execute();
You could use Bulk() http://docs.mongodb.org/manual/reference/method/Bulk/
With an ordered operations list, MongoDB executes the write operations in the list serially. If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.
Interesting Blog Article about the Bulk API: http://blog.mongodb.org/post/84922794768/mongodbs-new-bulk-api
I need help to get a best form to do a specific query in a Mongodb
I have a jobs collection being fed hourly, and want take some useful information:
The last 10 record by jobname and take a avg the timedur but remove the higher and lower value.
I can get 10 last records by timeend key
Thanks for helping me solve this problem.
{
"_id" : ObjectId("52446679e4b0961fd47b63a9"),
"jobname" : "ftp_s_jobx",
"descript" : "Get some file",
"applic" : "PRD.TEAM",
"applgroup" : "bil.jobx.set",
"schedtab" : "bil.jobx.set",
"owner" : "cdfiles",
"runcount" : "1",
"cyclic" : "Y",
"times" : [
{
"timeelep" : "3674"
},
{
"timestmp" : "20130926132537"
},
{
"timedur" : "00:00:36"
},
{
"timeend" : "26/09/2013 13:25:37"
}
]
}
I have the following object in my mongo database named music.
I want to update where the genre is Grunge
The band name is Nirvana
The album name is Nevermind
The track order is 1
and change the track's name to "Smells Like Teen Spirit!".
I've tried playing with the positional operator, but can't quite
figure this out.
{
genre : "Grunge",
bands : [ {
name : "Nirvana",
albums : [ {
name : "Nevermind",
tracks : [ {
name : "Smell Like Teen Spirit",
order : 1,
duration : 301
},
{
name : "In Bloom",
order : 2,
duration : 254
} ]
},
{
name : "In Utero",
tracks : [ {
name : "Server the Servants",
order : 1,
duration : 216
},
{
name : "Scentless Apprentice",
order : 2,
duration : 254
} ]
} ]
},
{
name : "Karma++ : A Nirvina Tribute Band",
albums : [ {
name : "Nevermind",
tracks : [ {
name : "Smell Like Teen Spirit",
order : 1,
duration : 301
},
{
name : "In Bloom",
order : 2,
duration : 254
} ]
},
{
name : "In Utero",
tracks : [ {
name : "Server the Servants",
order : 1,
duration : 216
},
{
name : "Scentless Apprentice",
order : 2,
duration : 254
} ]
} ]
} ]
}
Unfortunately, at present it is only possible to use a single "$" positional per update. This limits the update to a single embedded array, similar to the example in the documentation: http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
(From your post, it looks like you have already found this, but I have included the link for the benefit of any other users reading this post.)
In order to make the update, you will have to know the position of two out of the following three: The position of the band in the "bands" array, the position of the album in the albums array, or the position of the track in the "tracks" array.
There is a feature request for this functionality, and it is slated for version 2.3.0 (although this is subject to change).
https://jira.mongodb.org/browse/SERVER-831 "Positional Operator Matching Nested Arrays"
For the time being, you will have to know the position of the sub documents in two out of the three arrays:
db.music.update({genre : "Grunge", "bands.name" : "Nirvana"}, {$set:{"bands.$.albums.0.tracks.0.name":"Smells Like Teen Spirit!"}})
db.music.update({genre : "Grunge", "bands.0.albums.name" : "Nevermind"}, {$set:{"bands.0.albums.$.tracks.0.name":"Smells Like Teen Spirit!"}})
or
db.music.update({genre : "Grunge", "bands.0.albums.0.tracks.order" : 1}, {$set:{"bands.0.albums.0.tracks.$.name":"Smells Like Teen Spirit!"}})