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 .
Related
I'm trying to execute a query like:
{array.0.property: {$ne: null}}.
It return nothing even if all documents have this property different from null.
After some tests i noticed that it work using $elemMatch, but i need to query only for the first element of the array.
The first element is to be considered as "Master" where all query should search.
I can't change document "schema".
Anyone know ho to solve this problem?
I'm using Mongodb 3.6.8.
Thanks in advice.
Example query:
db.getCollection('tasks').find({'details.0.code': {$ne: null}});
Example documents:
{
"name": "test",
"date": 2018-07-17 06:30:00.000Z,
.....,
"details": [
{
"code": '123',
"description": 'something',
"resolutionYear": 2018
},
{
"code": null,
"description": 'secondary',
"resolutionYear": 2019
}
]
},
{
"name": "exam",
"date": 2018-09-20 09:00:00.000Z,
.....,
"details": [
{
"code": null,
"description": 'exam',
"resolutionYear": null
}
]
}
Let's imagine a mongo collection of - let's say magazines. For some reason, we've ended up storing each issue of the magazine as a separate document. Each article is a subdocument inside an Articles-array, and the authors of each article is represented as a subdocument inside the Writers-array on the Article-subdocument. Only the name and email of the author is stored inside the article, but there is an Writers-array on the magazine level containing more information about each author.
{
"Title": "The Magazine",
"Articles": [
{
"Title": "Mongo Queries 101",
"Summary": ".....",
"Writers": [
{
"Name": "tom",
"Email": "tom#example.com"
},
{
"Name": "anna",
"Email": "anna#example.com"
}
]
},
{
"Title": "Why not SQL instead?",
"Summary": ".....",
"Writers": [
{
"Name": "mike",
"Email": "mike#example.com"
},
{
"Name": "anna",
"Email": "anna#example.com"
}
]
}
],
"Writers": [
{
"Name": "tom",
"Email": "tom#example.com",
"Web": "tom.example.com"
},
{
"Name": "mike",
"Email": "mike#example.com",
"Web": "mike.example.com"
},
{
"Name": "anna",
"Email": "anna#example.com",
"Web": "anna.example.com"
}
]
}
How can one author be completely removed from a magazines?
Finding magazines where the unwanted author exist is quite easy. The problem is pulling the author out of all the sub documents.
MongoDB 3.6 introduces some new placeholder operators, $[] and $[<identity>], and I suspect these could be used with either $pull or $pullAll, but so far, I haven't had any success.
Is it possible to do this in one go? Or at least no more than two? One query for removing the author from all the articles, and one for removing the biography from the magazine?
You can try below query.
db.col.update(
{},
{"$pull":{
"Articles.$[].Writers":{"Name": "tom","Email": "tom#example.com"},
"Writers":{"Name": "tom","Email": "tom#example.com"}
}},
{"multi":true}
);
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.
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
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.