In my scenerio, there are authors in a collection, each author has messages and each message of author can has events. Each actor allowed to perform only one kind of action once.
db.people.ensureIndex({messages.messageEvents.eventName: 1, messages.messageEvents.actorId: 1}, {unique: true});
I added index but it has no effect. As you see below, my document has three elements which have "eventName":"vote" and "actorId":"1234" that should be against my constraint.
How to ensure unique item in messageEvents array based on eventName and actorId fields ?
Actually, i need to update the existing item without a second search and update event instead of rejecting it .
{
"_id": "1234567",
"authorPoint": 0,
"messages": [
{
"messageId": "112",
"messageType": "Q",
"messagePoint": 0,
"messageEvents": [
{
"eventName": "Add",
"actorId": "1234",
"detail": ""
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "up"
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "down"
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "cork"
}
]
}
]
}
Mustafa, unique constraints are not enforced within a single array, although they're enforced among documents in a collection. This is a known bug that won't be fixed for a while:
https://jira.mongodb.org/browse/SERVER-1068
There's a workaround, though. Keep your unique index in place, and:
1) Ensure your application does not insert new documents with duplicate values in the array. You can check for uniqueness in your application code before inserting.
2) When updating existing documents use $addToSet instead of $push.
Related
I am trying to figure out specific mongoDb query, so far unsuccessfully.
Documents in my collections looks someting like this (contain more attributes, which are irrelevant for this query):
[{
"_id": ObjectId("596e01b6f4f7cf137cb3d096"),
"code": "A",
"name": "name1",
"sys": {
"cts": ISODate("2017-07-18T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d097"),
"code": "A",
"name": "name2",
"sys": {
"cts": ISODate("2017-07-19T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d098"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-16T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d099"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-10T12:40:22.772Z"),
}
}]
What I need is to get current versions of documents, filtered by code or name, or both. Current version means that from two(or more) documents with same code, I want pick the one which has latest sys.cts date value.
So, result of this query executed with filter name="name3" would be the 3rd document from previous list. Result of query without any filter would be 2nd and 3rd document.
I have an idea how to construct this query with changed data model but I was hoping someone could lead me right way without doing so.
Thank you
I have some mongodb documents which structure like:
{
"_id": ObjectId("58c212b06ca3472b902f9fdb"),
"Auction name": "Building",
"Estimated price": "23,660,000",
"Auction result": "success",
"Url": "https://someurl.htm",
"match_id": "someid",
"Final price": "17,750,000",
"Area": [
{
"Area": "696.77"
}
]
}
The "match_id" is used for update query and after that I don't need this entry anymore.
Is there any idea to drop this entry and keep the rest of the document?
Have you tried simpily using an update query to unset the field like the following
db.products.update(
{},
{ $unset: { match_id: "" } }
)
Keep in mind that the first set of curly braces has been intentionally left blank so that your update query matches every entry in your collection
How can I get mlab/mongodb to stop autogenerating one of these IDs?
key: { : ObjectId('584f7e76fd51ea874cee3c70') }",
"op":{"title":"Gold (feat. Lil Wayne) (Remix)",
"artist":"kiiara",
"audio":"https://api.soundcloud.com/tracks/293663905/stream?client_id=90d140308348273b897fab79f44a7c89",
"image":"https://i1.sndcdn.com/artworks-C09aBfgKDpCI-0-t500x500.jpg",
"download":"https://api.soundcloud.com/tracks/293663905/download",
"url":"https://soundcloud.com/kiiaraonline/gold-lil-wayne-remix",
"created":"2016-12-13T04:52:06.309Z",
"genres":{"hipHop":30,"house":20,"pop":50},
"_id":"584f7e76fd51ea874cee3c70"}
When I'm trying to create a relationship I get an error saying there are two keys. And I think that is because there are two IDs. How can I remove one of them? Preferably the ObjectId one.
The strange thing is, when I view them in the database I only see this
{
"_id": {
"$oid": "584f7e76fd51ea874cee3c70"
},
"title": "Gold (feat. Lil Wayne) (Remix)",
"artist": "kiiara",
"audio": "https://api.soundcloud.com/tracks/293663905/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-C09aBfgKDpCI-0-t500x500.jpg",
"download": "https://api.soundcloud.com/tracks/293663905/download",
"url": "https://soundcloud.com/kiiaraonline/gold-lil-wayne-remix",
"created": {
"$date": "2016-12-13T04:52:06.309Z"
},
"genres": {
"hipHop": 30,
"house": 20,
"pop": 50
}
}
Thank you
Use $exists in update as below :
db.collectioName.update({"key":{"$exists":true}},{"$unset":{"key":""}},
{"upsert":false,"multi":true})
unset remove key fields in all matching documents .
mydata is look like below:-
{
"categories": [
{
"categoryname": "Eletronics",
"categoryId": "89sxop",
"displayname": "Eletronics",
"subcategories": [
{
"subcategoryname": "laptop",
"subcategoryId": "454",
"displayname": "Laptop"
},
{
"subcategoryname": "camera",
"subcategoryId": "sony123",
"displayname": "Camera"
}
]
}
]
}
I want to delete a specific object from a Subcategories Array
we are Trying like below code:-(This is to Delete Category)
val removingData = $pull(MongoDBObject("categories" -> MongoDBObject("categoryName" -> "Entertainment")))
this code is for removing Particular category.
But I Want To Remove ONE or MORE subCategories from particular category.
The subCategory(Ex:-i want to Delete camera Object from mydata) from the Electronics category
Expected Output:-(after deleting the camera Object)
{
"categories": [
{
"categoryname": "Eletronics",
"categoryId": "89sxop",
"displayname": "Eletronics",
"subcategories": [
{
"subcategoryname": "laptop",
"subcategoryId": "454",
"displayname": "Laptop"
}
]
}
]
}
Best Regards
GSY
You need to use the $pull operator, specifying the subcategories array and the condition used to pick the elements to remove. The $ operator can be used to select the subcategories array of the specific categories element you're interested in. See the Update Array Operators docs of mongo for details. Something like the following should work:
val query = MongoDBObject("categories.categoryname" -> "Electronics")
val removingData = $pull("categories.$.subcategories" -> MongoDBObject("subcategoryname" -> "camera"))
collection.update(query, removingData)
If you want to remove multiple subcategories, you can use the $or operator to specify multiple subcategorynames
In my scenerio, there are authors in a collection, each author has messages and each message of author can has events. Each actor allowed to perform only one kind of action once.
db.people.ensureIndex({messages.messageEvents.eventName: 1, messages.messageEvents.actorId: 1}, {unique: true});
I added index but it has no effect. As you see below, my document has three elements which have "eventName":"vote" and "actorId":"1234" that should be against my constraint.
How to ensure unique item in messageEvents array based on eventName and actorId fields ?
Actually, i need to update the existing item without a second search and update event instead of rejecting it .
{
"_id": "1234567",
"authorPoint": 0,
"messages": [
{
"messageId": "112",
"messageType": "Q",
"messagePoint": 0,
"messageEvents": [
{
"eventName": "Add",
"actorId": "1234",
"detail": ""
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "up"
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "down"
},
{
"eventName": "Vote",
"actorId": "1234",
"detail": "cork"
}
]
}
]
}
Mustafa, unique constraints are not enforced within a single array, although they're enforced among documents in a collection. This is a known bug that won't be fixed for a while:
https://jira.mongodb.org/browse/SERVER-1068
There's a workaround, though. Keep your unique index in place, and:
1) Ensure your application does not insert new documents with duplicate values in the array. You can check for uniqueness in your application code before inserting.
2) When updating existing documents use $addToSet instead of $push.