Copy a field from one collection to another in mongodb with foreign key as mixed type
Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name
from userdetails
collection to user
collection where userId in userDetails equals _id in user.
user collection
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
userDetail collection
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
Here is my query :
db.userDetails.find().forEach(
function(x)
db.user.update( _id :x.userId, $set: name:x.name);
);
This query is partially working. It only updates user
documents where _id is of type string. User document with _id as ObjectId are not getting updated.
database mongodb mongodb-query
add a comment |
Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name
from userdetails
collection to user
collection where userId in userDetails equals _id in user.
user collection
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
userDetail collection
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
Here is my query :
db.userDetails.find().forEach(
function(x)
db.user.update( _id :x.userId, $set: name:x.name);
);
This query is partially working. It only updates user
documents where _id is of type string. User document with _id as ObjectId are not getting updated.
database mongodb mongodb-query
Ye Olde JavaScript hack._id: new ObjectId(x.userId.valueOf())
As thevalueOf()
is going to return the "string" whether it's a string or anObjectId
. And of course we're casting toObjectId
. Strongly Advising that you actually at least normalize ALL_id
values to beObjectId
. And that means deleting the documents where it is not since_id
is immutable.
– Neil Lunn
Nov 12 at 11:36
add a comment |
Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name
from userdetails
collection to user
collection where userId in userDetails equals _id in user.
user collection
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
userDetail collection
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
Here is my query :
db.userDetails.find().forEach(
function(x)
db.user.update( _id :x.userId, $set: name:x.name);
);
This query is partially working. It only updates user
documents where _id is of type string. User document with _id as ObjectId are not getting updated.
database mongodb mongodb-query
Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name
from userdetails
collection to user
collection where userId in userDetails equals _id in user.
user collection
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
userDetail collection
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
Here is my query :
db.userDetails.find().forEach(
function(x)
db.user.update( _id :x.userId, $set: name:x.name);
);
This query is partially working. It only updates user
documents where _id is of type string. User document with _id as ObjectId are not getting updated.
database mongodb mongodb-query
database mongodb mongodb-query
asked Nov 12 at 11:16
truekiller
12612
12612
Ye Olde JavaScript hack._id: new ObjectId(x.userId.valueOf())
As thevalueOf()
is going to return the "string" whether it's a string or anObjectId
. And of course we're casting toObjectId
. Strongly Advising that you actually at least normalize ALL_id
values to beObjectId
. And that means deleting the documents where it is not since_id
is immutable.
– Neil Lunn
Nov 12 at 11:36
add a comment |
Ye Olde JavaScript hack._id: new ObjectId(x.userId.valueOf())
As thevalueOf()
is going to return the "string" whether it's a string or anObjectId
. And of course we're casting toObjectId
. Strongly Advising that you actually at least normalize ALL_id
values to beObjectId
. And that means deleting the documents where it is not since_id
is immutable.
– Neil Lunn
Nov 12 at 11:36
Ye Olde JavaScript hack.
_id: new ObjectId(x.userId.valueOf())
As the valueOf()
is going to return the "string" whether it's a string or an ObjectId
. And of course we're casting to ObjectId
. Strongly Advising that you actually at least normalize ALL _id
values to be ObjectId
. And that means deleting the documents where it is not since _id
is immutable.– Neil Lunn
Nov 12 at 11:36
Ye Olde JavaScript hack.
_id: new ObjectId(x.userId.valueOf())
As the valueOf()
is going to return the "string" whether it's a string or an ObjectId
. And of course we're casting to ObjectId
. Strongly Advising that you actually at least normalize ALL _id
values to be ObjectId
. And that means deleting the documents where it is not since _id
is immutable.– Neil Lunn
Nov 12 at 11:36
add a comment |
1 Answer
1
active
oldest
votes
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8
not a mongo _id) and use this query:
let usersIds = ;
db.user.find("_id": $type: 7).forEach(doc =>
usersIds.push(doc._id + '')
db.userDetail.find(
userId:
$in: usersIds
).forEach(doc =>
db.user.update(
"_id": ObjectId(doc.userId)
,
$set:
"name": doc.name
,
multi: false,
upsert: false
)
)
)
if you have any question feel free to ask
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261010%2fcopy-a-field-from-one-collection-to-another-in-mongodb-with-foreign-key-as-mixed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8
not a mongo _id) and use this query:
let usersIds = ;
db.user.find("_id": $type: 7).forEach(doc =>
usersIds.push(doc._id + '')
db.userDetail.find(
userId:
$in: usersIds
).forEach(doc =>
db.user.update(
"_id": ObjectId(doc.userId)
,
$set:
"name": doc.name
,
multi: false,
upsert: false
)
)
)
if you have any question feel free to ask
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
add a comment |
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8
not a mongo _id) and use this query:
let usersIds = ;
db.user.find("_id": $type: 7).forEach(doc =>
usersIds.push(doc._id + '')
db.userDetail.find(
userId:
$in: usersIds
).forEach(doc =>
db.user.update(
"_id": ObjectId(doc.userId)
,
$set:
"name": doc.name
,
multi: false,
upsert: false
)
)
)
if you have any question feel free to ask
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
add a comment |
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8
not a mongo _id) and use this query:
let usersIds = ;
db.user.find("_id": $type: 7).forEach(doc =>
usersIds.push(doc._id + '')
db.userDetail.find(
userId:
$in: usersIds
).forEach(doc =>
db.user.update(
"_id": ObjectId(doc.userId)
,
$set:
"name": doc.name
,
multi: false,
upsert: false
)
)
)
if you have any question feel free to ask
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8
not a mongo _id) and use this query:
let usersIds = ;
db.user.find("_id": $type: 7).forEach(doc =>
usersIds.push(doc._id + '')
db.userDetail.find(
userId:
$in: usersIds
).forEach(doc =>
db.user.update(
"_id": ObjectId(doc.userId)
,
$set:
"name": doc.name
,
multi: false,
upsert: false
)
)
)
if you have any question feel free to ask
edited Nov 12 at 12:40
answered Nov 12 at 12:07
andranikasl
28924
28924
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
add a comment |
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
Some of the documents in user tables have application generated Id and i can not change those id. This is where all the complication lies. Else the query i have mentioned in the question would have worked.
– truekiller
Nov 12 at 12:16
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
This query doesn't work. Nothing changed.
– truekiller
Nov 12 at 12:22
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
in this case you can run two different queries: first your query for string _id's second second for objectId's Please check updated answer
– andranikasl
Nov 12 at 12:28
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
I hope this will help you to solve your question
– andranikasl
Nov 12 at 12:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261010%2fcopy-a-field-from-one-collection-to-another-in-mongodb-with-foreign-key-as-mixed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Ye Olde JavaScript hack.
_id: new ObjectId(x.userId.valueOf())
As thevalueOf()
is going to return the "string" whether it's a string or anObjectId
. And of course we're casting toObjectId
. Strongly Advising that you actually at least normalize ALL_id
values to beObjectId
. And that means deleting the documents where it is not since_id
is immutable.– Neil Lunn
Nov 12 at 11:36